Skip to content

Commit

Permalink
clean up just file
Browse files Browse the repository at this point in the history
  • Loading branch information
mimoo committed Oct 15, 2024
1 parent 74cb6dd commit f48d988
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/check_n_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Get dependencies
- name: Get just
uses: extractions/setup-just@v1
- uses: actions/setup-python@v5

- name: Get pip
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip" # caching pip dependencies
- run: pip install -r requirements.txt

- name: Build project
env:
BASE_URL: "/RFCs"
run: |
just setup
just build
- name: Deploy
Expand Down
7 changes: 3 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

setup:
pip install -r requirements.txt
mkdir -p rfcs/starknet/

build:
mkdir -p rfcs/starknet/
python md2respec.py source/starknet/fri.md > rfcs/starknet/fri.html
python md2respec.py source/starknet/channel.md > rfcs/starknet/channel.html
python md2respec.py --output-path rfcs/ --recursive ./source/

debug-build:
python md2respec.py --pure-html source/starknet/fri.md
Expand All @@ -14,4 +13,4 @@ serve:
python -m http.server

watch:
just serve & watchexec -w md2respec.py -w source/starknet/fri.md -w template.html just build
just serve & watchexec -w md2respec.py -w source/ -w template.html just build
92 changes: 66 additions & 26 deletions md2respec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import markdown
import re
from string import Template
from pathlib import Path

template_path = "template.html"

Expand Down Expand Up @@ -121,32 +122,6 @@ def html_to_respec(metadata, html_content):
return template.substitute(metadata)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("input_path", help="markdown input file")
parser.add_argument("--output-path", default="spec.html", help="HTML output file")
parser.add_argument("--pure-html", action="store_true", help="Output pure HTML")
args = parser.parse_args()

# ensure that input path is a .md file
if not args.input_path.endswith(".md"):
raise ValueError("Input file must be a markdown file")

# ensure that output path is a.html file
if not args.output_path.endswith(".html"):
raise ValueError("Output file must be an HTML file")

# Example usage
config = {"section_headers": True} # Enable section headers

metadata, html_output = convert_markdown_to_html(args.input_path, config)

if not args.pure_html:
html_output = html_to_respec(metadata, html_output)

print(html_output)


class Latex:
_single_dollar_re = re.compile(r"(?<!\$)\$(?!\$)(.*?)\$")
_double_dollar_re = re.compile(r"\$\$(.*?)\$\$", re.DOTALL)
Expand Down Expand Up @@ -197,5 +172,70 @@ def run(self, text):
return text


def recursive_folder_conversion(input_dir, output_dir):
if not os.path.isdir(input_dir):
raise ValueError(
"Input path must be a directory when using the --recursive option"
)
if not os.path.isdir(output_dir):
raise ValueError(
"Output path must be a directory when using the --recursive option"
)

for md_file in Path(input_dir).rglob("*.md"):
input_path = str(md_file)

# preserve path
relative_path = os.path.relpath(md_file, input_dir)
output_path = os.path.join(
output_dir, os.path.splitext(relative_path)[0] + ".html"
)

# convert
config = {"section_headers": True} # Enable section headers
metadata, html_output = convert_markdown_to_html(input_path, config)
html_output = html_to_respec(metadata, html_output)
with open(output_path, "w") as f:
f.write(html_output)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("input_path", help="markdown input file")
parser.add_argument("--output-path", default="spec.html", help="HTML output file")
parser.add_argument("--pure-html", action="store_true", help="Output pure HTML")
parser.add_argument(
"--recursive",
action="store_true",
help="Convert all markdown files in a folder",
)
args = parser.parse_args()

if args.recursive:
if args.pure_html:
raise ValueError(
"Cannot use the --pure-html option with the --recursive option"
)
recursive_folder_conversion(args.input_path, args.output_path)
return

# ensure that input path is a .md file
if not args.input_path.endswith(".md"):
raise ValueError("Input file must be a markdown file")

# ensure that output path is a.html file
if not args.output_path.endswith(".html"):
raise ValueError("Output file must be an HTML file")

# convert
config = {"section_headers": True} # Enable section headers
metadata, html_output = convert_markdown_to_html(args.input_path, config)

if not args.pure_html:
html_output = html_to_respec(metadata, html_output)

print(html_output)


if __name__ == "__main__":
main()

0 comments on commit f48d988

Please sign in to comment.