Skip to content

Commit

Permalink
add script to remove admonitions and run it during pdf build
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Feb 23, 2024
1 parent f90fd70 commit 224ad94
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ venvs
pdf_build_src/bids-spec.pdf
pdf_build_src/bids-spec_pandoc_log.json
pdf_build_src/src_copy
pdf_build_src/tests/data/output

# JS/NPM
package-lock.json
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ markdown_extensions:
- toc:
anchorlink: true
- pymdownx.superfences
- admonition
- pymdownx.details
plugins:
- search
- branchcustomization:
Expand Down
22 changes: 14 additions & 8 deletions pdf_build_src/process_markdowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import numpy as np

from remove_admonitions import remove_admonitions

sys.path.append("../tools/")
# functions from module macros are called by eval() later on
from mkdocs_macros_bids import macros # noqa: F401
Expand Down Expand Up @@ -679,33 +681,37 @@ def process_macros(duplicated_src_dir_path):

duplicated_src_dir_path = "src_copy/src"

# Step 1: make a copy of the src directory in the current directory
# make a copy of the src directory in the current directory
copy_src()

# Step 2: run mkdocs macros embedded in markdown files
# run mkdocs macros embedded in markdown files
process_macros(duplicated_src_dir_path)

# Step 3: copy BIDS_logo to images directory of the src_copy directory
# remove mkdocs admonition
remove_admonitions(input_folder=duplicated_src_dir_path,
output_folder=duplicated_src_dir_path)

# copy BIDS_logo to images directory of the src_copy directory
copy_bids_logo()

# Step 4: copy images from subdirectories of src_copy directory
# copy images from subdirectories of src_copy directory
copy_images(duplicated_src_dir_path)
subprocess.call("mv src_copy/src/images/images/* src_copy/src/images/", shell=True)

# Step 5: extract the latest version number, date and title
# extract the latest version number, date and title
extract_header_string()
add_header()

edit_titlepage()

# Step 6: modify changelog to be a level 1 heading to facilitate section
# modify changelog to be a level 1 heading to facilitate section
# separation
modify_changelog()

# Step 7: remove all internal links
# remove all internal links
assert_no_multiline_links(duplicated_src_dir_path)
remove_internal_links_inline(duplicated_src_dir_path)
remove_internal_links_reference(duplicated_src_dir_path)

# Step 8: correct number of dashes and fences alignment for rendering tables in PDF
# correct number of dashes and fences alignment for rendering tables in PDF
correct_tables(duplicated_src_dir_path)
55 changes: 55 additions & 0 deletions pdf_build_src/remove_admonitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Script to remove all mkdocs admonition from the markdown files in a directory."""
from __future__ import annotations
from pathlib import Path
from rich import print
import shutil

INDENT = " "


def remove_admonitions(input_folder: str | Path, output_folder: str | Path, indent: str = None):

if indent is None:
indent = INDENT

md_files = Path(input_folder).glob("**/*.md")

for file in md_files:

with open(file, "r", encoding="utf8") as f:
content = f.readlines()

output_file = Path(output_folder) / file.relative_to(input_folder)
output_file.parent.mkdir(parents=True, exist_ok=True)
print(f"processing: {file}\n to: {output_file}")

with open(output_file, "w", encoding="utf8") as f:

is_admonition = False
counter = 0
for line in content:

if line.startswith("!!!"):
is_admonition = True
counter = 0
continue

# skip first line after admonition
if is_admonition and counter == 0:
counter += 1
continue

if not line.startswith(indent):
is_admonition = False

if is_admonition:
line = line.lstrip(indent)

f.write(line)


if __name__ == "__main__":
input_folder = Path(__file__).parent / "tests" / "data" / "input"
output_folder = Path(__file__).parent / "tests" / "data" / "output"
shutil.rmtree(output_folder)
remove_admonitions(input_folder, output_folder)
13 changes: 13 additions & 0 deletions pdf_build_src/tests/data/expected/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Test inputs

This input directory contains data to use for testing the pdf build code of the BIDS specification.

For example the following admonition should be removed by `pdf_build_src/remove_admonitions.py`.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla et euismod nulla.
Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa,
nec semper lorem quam in massa.

The `expected` directory should contain the documents
as they should look like after processing.
15 changes: 15 additions & 0 deletions pdf_build_src/tests/data/input/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test inputs

This input directory contains data to use for testing the pdf build code of the BIDS specification.

For example the following admonition should be removed by `pdf_build_src/remove_admonitions.py`.

!!! note

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla et euismod nulla.
Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa,
nec semper lorem quam in massa.

The `expected` directory should contain the documents
as they should look like after processing.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Magnetic Resonance Imaging

## Common metadata fields

!!! warning "foo bar"

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla et euismod nulla.
Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa,
nec semper lorem quam in massa.
6 changes: 4 additions & 2 deletions src/modality-specific-files/near-infrared-spectroscopy.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Please see [Citing BIDS](../introduction.md#citing-bids)
on how to appropriately credit this extension when referring to it in the
context of the academic literature.

Several [example NIRS datasets](https://bids-standard.github.io/bids-examples/#nirs)
have been formatted using this specification and can be used for practical guidance when curating a new dataset.
!!! example "Example datasets"

Several [example NIRS datasets](https://bids-standard.github.io/bids-examples/#nirs)
have been formatted using this specification and can be used for practical guidance when curating a new dataset.

## NIRS recording data

Expand Down

0 comments on commit 224ad94

Please sign in to comment.