From 9ffbd064b188c668a9fa3f76c4fb4bf779f3a519 Mon Sep 17 00:00:00 2001 From: khamidou Date: Wed, 1 Sep 2010 20:16:49 +0200 Subject: [PATCH] finally got cross-references right --- pycco | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/pycco b/pycco index 21dcdbd..aaabb7b 100755 --- a/pycco +++ b/pycco @@ -70,19 +70,35 @@ def parse(source, code): save(docs_text, code_text) return sections -def preprocess(comment, section_nr): +# === Preprocessing the comments === +# # Add cross-references before having the text processed by markdown. -# It's possible to reference another file, like this : [[pycco.py]] or a specific function of -# another file, like this: [[file#func]] - def repl(match): - return "[%s](%s)" % (match.group(1), path.basename(destination(match.group(1)))) +# It's possible to reference another file, like this : [[pycco.py]] or a specific section of +# another file, like this: [[pycco.py#Highlighting]]. Of course, sections have to be manually declared before, +# A section name is written on a single line, and surrounded by equals signs, === like this === +def preprocess(comment, section_nr): + def sanitize_section_name(name): + return name.strip().split(" ")[0] - def repl2(match): - return "[%s](%s)" % (match.group(1), destination(match.group(1)) + "#section-" + str(section_nr)) + def replace_crossref(match): + # Check if the match contains an anchor + if '#' in match.group(1): + name, anchor = match.group(1).split('#') + return "[%s](%s#%s)" % (name, path.basename(destination(name)), anchor) + else: + return "[%s](%s)" % (match.group(1), path.basename(destination(match.group(1)))) - return re.sub('\[\[(.+)\]\]', repl, comment) + def replace_section_name(match): + return '*%s*' % (sanitize_section_name(match.group(1)), match.group(1)) + comment = re.sub('===(.+)===\\n', replace_section_name, comment) + comment = re.sub('\[\[(.+)\]\]', replace_crossref, comment) + + return comment + +# === Highlighting the source code === +# # Highlights a single chunk of code using the **Pygments** module, and runs the # text of its corresponding comment through **Markdown**. # @@ -103,6 +119,7 @@ def highlight(source, sections): section["docs_html"] = markdown(preprocess(section["docs_text"], i)) section["num"] = i +# === HTML Code generation === # Once all of the code is finished highlighting, we can generate the HTML file # and write out the documentation. Pass the completed sections into the template # found in `resources/pycco.html` -- 2.43.0