]> khamidou — GitHub mirrors - pycco.git/commitdiff
finally got cross-references right master
authorkhamidou <khamidou@gmail.com>
Wed, 1 Sep 2010 18:16:49 +0000 (20:16 +0200)
committerkhamidou <khamidou@gmail.com>
Wed, 1 Sep 2010 18:16:49 +0000 (20:16 +0200)
pycco

diff --git a/pycco b/pycco
index 21dcdbd687aa8bac342feec2485c8c39c08d9d3b..aaabb7b659b4dac86fd258e709c2959524a4e6a6 100755 (executable)
--- 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 '<a name="%s">*%s*</a>' % (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`