Last active
October 21, 2015 16:45
-
-
Save feiss/c83b46a500b4c882cfcb to your computer and use it in GitHub Desktop.
Very simple and basic documentation generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import re | |
def main(): | |
if len(sys.argv) < 2: | |
print 'specify input file' | |
exit(1) | |
fo = open(sys.argv[1] + '.html', 'w+') | |
fo.write(HEAD) | |
fo.write('<h1>' + sys.argv[1] + '</h1>') | |
with open(sys.argv[1]) as f: | |
lines = f.readlines() | |
comms = [] | |
blocks = [] | |
html = '' | |
for line in lines: | |
line = line.strip('\n{ ') | |
if line[:4] == '///@': | |
html = html + '<h2 id="' + line[4:] + '">' + line[4:] + '<a href="#">^</a></h2>\n' | |
blocks.append(line[4:]) | |
if line[:4] == '/// ': | |
comms.append(re.sub(r"(\$\w+)", lambda x: '<b>' + x.group(0) + '</b>', line[4:])) | |
if line[:8] == 'function': | |
html = html + '<code>' + formatFunction(line[9:]) + '</code>\n' | |
html = html + '<dd>'+(' '.join(comms)).capitalize()+'</dd>' | |
comms = [] | |
if line[:5] == 'class': | |
html = html + '<code class="class">' + formatFunction(line) + '</code>\n' | |
html = html + '<dd>'+(' '.join(comms)).capitalize()+'</dd>' | |
comms = [] | |
if line.find('public function') != -1: | |
html = html + '<code>' + formatFunction(line[line.find('function')+9:]) + '</code>\n' | |
html = html + '<dd>'+(' '.join(comms)).capitalize()+'</dd>' | |
comms = [] | |
fo.write('<header>') | |
for block in blocks: | |
fo.write('<a href="#' + block + '">' + block + '</a> ') | |
fo.write('</header>') | |
fo.write(html) | |
fo.write(getFOOTER()) | |
def formatFunction(line): | |
line = re.sub(r"\$", " <b>$", line) | |
line = re.sub(r",", "</b>, ", line) | |
line = re.sub(r"(=\w+)(\(\))?", lambda x: '<i>' + x.group(0) + '</i>', line) | |
if (line.find('$') != -1): | |
line = re.sub(r"\)$", "</b> )", line) | |
return line | |
HEAD= '''<!doctype html> | |
<html lang="en"> | |
<head> | |
<style> | |
body, html{ | |
font: 14px "Open Sans", monospace, sans-serif; | |
} | |
dd{ | |
display: block; | |
margin: 0; | |
color: #555; | |
margin-left: 1em; | |
} | |
dd b{ | |
color: #85007A; | |
font: 14px monospace, sans-serif; | |
font-weight: bold; | |
} | |
h2{ | |
font-size: 30px; | |
font-weight: 100; | |
color: #666; | |
margin-top: 2em; | |
} | |
h2 a{ | |
font-size: 18px; | |
float: right; | |
} | |
code{ | |
font-weight: bold; | |
display: block; | |
color: #103CA2; | |
margin: 2em 0 0.5em 0; | |
border: 1px solid #D0E2FD; | |
border-bottom: none; | |
clear: both; | |
padding: 0.6em 1em; | |
background: #EBF3FF; | |
} | |
code.class{ | |
font-size: 1.2em; | |
margin-top: 3em; | |
} | |
code b{ | |
color: #85007A; | |
} | |
code b i{ | |
color: #777; | |
} | |
a{ | |
display: inline-block; | |
margin-right: 0.5em; | |
color: #103CA2; | |
text-decoration: none; | |
} | |
a:hover{ | |
text-decoration: underline; | |
} | |
</style> | |
</head> | |
<body> | |
''' | |
def getFOOTER(): | |
return '</body>\n</html>' | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment