|
| 1 | +# Run this as a pre-commit script. |
| 2 | +# Will be a no-op unless docs have changed. |
| 3 | +# |
| 4 | +# Takes readme-generator.md and creates: |
| 5 | +# 1. A jekyll-ready markdown file with includes for dynamic github pages |
| 6 | +# 2. A github-ready markdown file with images for static pages |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def initialize_dynamic_lines(): |
| 13 | + return [ |
| 14 | + '---\n', |
| 15 | + 'layout: default\n', |
| 16 | + '---\n' |
| 17 | + ] |
| 18 | + |
| 19 | + |
| 20 | +def initialize_static_lines(): |
| 21 | + return [ |
| 22 | + '# A lightweight HTML+JS library for inputting two-dimensional form data\n\n', |
| 23 | + 'See this project at [https://brmighell.github.io/rcv-entry/]\n\n', |
| 24 | + ] |
| 25 | + |
| 26 | + |
| 27 | +def isFileDataEqual(filename, expectedlines): |
| 28 | + # Is expectedlines equal to filename? |
| 29 | + with open(filename, 'r') as f: |
| 30 | + lines = f.readlines() |
| 31 | + return lines == expectedlines |
| 32 | + |
| 33 | + |
| 34 | +def create_derived_files(input_filename, output_static_filename, output_dynamic_filename): |
| 35 | + # Map from a magic key to a triple: |
| 36 | + # 1. variable name (just a helper - must match the key) |
| 37 | + # 2. What file to include in the dynamic file? |
| 38 | + # 3. What file to include in the static file? |
| 39 | + magic_keys = { |
| 40 | + '{{ deps }}\n': ('deps', 'docs/deps.html', None), |
| 41 | + '{{ ex0 }}\n': ('ex0', 'docs/github-pages-example-1.html', None), |
| 42 | + '{{ ex1 }}\n': ('ex1', 'docs/github-pages-example-1.html', None), |
| 43 | + '{{ ex2 }}\n': ('ex2', 'docs/github-pages-example-2.html', None), |
| 44 | + } |
| 45 | + |
| 46 | + # Small enough to just read it all into memory, |
| 47 | + # why complicate things? |
| 48 | + with open(input_filename, 'r') as f: |
| 49 | + lines = f.readlines() |
| 50 | + |
| 51 | + static_lines = initialize_static_lines() |
| 52 | + dynamic_lines = initialize_dynamic_lines() |
| 53 | + for line in lines: |
| 54 | + if line not in magic_keys: |
| 55 | + static_lines.append(line) |
| 56 | + dynamic_lines.append(line) |
| 57 | + continue |
| 58 | + |
| 59 | + # Magic keyword found. Do magic for static or dynamic |
| 60 | + (key, dynamic_content, static_content) = magic_keys[line] |
| 61 | + if static_content is not None: |
| 62 | + # Replace the static content |
| 63 | + static_lines.append( |
| 64 | + f'\n[\[interactive demo\]](https://artoonie.github.io/timeline-range-slider)\n') |
| 65 | + static_lines.append(f'\n') |
| 66 | + |
| 67 | + # Include the actual content + the dynamic content |
| 68 | + include_line = '{% capture ' + key + ' %}' |
| 69 | + include_line += '{% include_relative ' + dynamic_content + ' %}' |
| 70 | + include_line += '{% endcapture %}\n' |
| 71 | + dynamic_lines.append(include_line) |
| 72 | + dynamic_lines.append(line) |
| 73 | + |
| 74 | + # Safety check: don't overwrite accidental changes |
| 75 | + stream = os.popen('git diff --name-only') |
| 76 | + diffs = stream.read() |
| 77 | + |
| 78 | + # Make sure we didn't accidentally change README.md or index.md |
| 79 | + if output_dynamic_filename in diffs: |
| 80 | + if not isFileDataEqual(output_dynamic_filename, dynamic_lines): |
| 81 | + print( |
| 82 | + f"File {output_dynamic_filename} is dirty. Refusing to overwrite.") |
| 83 | + sys.exit(-1) |
| 84 | + if output_static_filename in diffs: |
| 85 | + if not isFileDataEqual(output_static_filename, static_lines): |
| 86 | + print( |
| 87 | + f"File {output_static_filename} is dirty. Refusing to overwrite.") |
| 88 | + sys.exit(-1) |
| 89 | + |
| 90 | + # Finally, write it all |
| 91 | + with open(output_static_filename, 'w') as f: |
| 92 | + f.write(''.join(static_lines)) |
| 93 | + with open(output_dynamic_filename, 'w') as f: |
| 94 | + f.write(''.join(dynamic_lines)) |
| 95 | + |
| 96 | + |
| 97 | +create_derived_files('docs/readme-generator.md', 'README.md', 'index.md') |
0 commit comments