Skip to content

Instantly share code, notes, and snippets.

@PeterJCLaw
Created October 16, 2022 22:34
Show Gist options
  • Save PeterJCLaw/318d392c46daff36577df5e24866d5b0 to your computer and use it in GitHub Desktop.
Save PeterJCLaw/318d392c46daff36577df5e24866d5b0 to your computer and use it in GitHub Desktop.
Extract code samples from SR docs
#!/bin/bash
git ls -r |\
grep '\.md$' |\
grep -vP '(troubleshooting/python.md|programming/sr/(compass|radio)/index.md)' |\
xargs ./get-code-chunks.py |\
grep -vP 'R\..+\.something(\.\.\.)?$' > code-examples.py
#!/usr/bin/env python3
import argparse
import subprocess
from pathlib import Path
import pandocfilters as pf # https://pypi.org/project/pandocfilters/
def code_blocks(key, value, format, meta):
if key != 'CodeBlock':
return {}
(_, tags, _), content = value
if 'python' in tags:
print(content)
print()
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
'file_paths',
metavar='file_path',
type=Path,
nargs=argparse.ONE_OR_MORE,
)
return parser.parse_args()
def main(args: argparse.Namespace) -> None:
for path in args.file_paths:
ast_source = subprocess.check_output([
'pandoc',
'-t',
'json',
'-s',
str(path),
])
print(f'# {path}\n')
pf.applyJSONFilters([code_blocks], ast_source, '')
print()
if __name__ == '__main__':
main(parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment