Skip to content

Instantly share code, notes, and snippets.

@zoeyfyi
Created April 3, 2017 16:04
Show Gist options
  • Save zoeyfyi/fdd8e8bb608921c0c7ccd8c2f11b6075 to your computer and use it in GitHub Desktop.
Save zoeyfyi/fdd8e8bb608921c0c7ccd8c2f11b6075 to your computer and use it in GitHub Desktop.
import re
def get_imports(source):
imports = []
i = 0
while i < len(source)-1:
print i
# Find the next import
i = source.find("#import", i, len(source))
if i == -1:
break
clip_start = i
i += + len("#import")
# Start clip
import_value = ""
# Skip any whitespave
while source[i] == ' ':
i += 1
# Match "
print i
assert source[i] == '"'
# Extract value
i += 1
import_value = ""
while source[i] != '"':
import_value += source[i]
i += 1
i += 1
# End clip
clip_end = i
print source[clip_start:clip_end]
imports.append((clip_start, clip_end, import_value))
return imports
source = ""
with open('src/doc.md') as source_doc:
source = source_doc.read()
imports = get_imports(source)
offset = 0
for (start, end, value) in imports:
value = 'src/' + value
print "Importing: {}".format(value)
with open(value, 'r') as import_file:
insert = import_file.read()
source = source[:start-offset] + insert + source[end-offset:]
offset += end - start - len(insert)
with open('doc_gen.md', 'w') as doc:
doc.write(source)
@tpb1908
Copy link

tpb1908 commented Apr 11, 2017

You might not need it, but I added a few extras to the hook to save me typing so much.
As well as adding the file type for highlighting, this adds the filename in bold above the code block, which saves writing it out.

It will also import single functions or subclasses, given their name and modifiers.
There's also #page which adds "<div style="page-break-after: always;">" to add a page break for PDFs.

https://gist.github.com/tpb1908/2e5b2eb2be50cdc95ed258d1dcad2b00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment