From c62d1600d7da7f9e4bb7432a4b659a9cb67ea253 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 29 Jun 2010 21:19:30 -0700 Subject: [PATCH] Updating index.html again.. --- index.html | 129 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 50 deletions(-) diff --git a/index.html b/index.html index 59c56bb..1f1e9d6 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - pocco.py + pocco @@ -12,7 +12,7 @@ - + @@ -22,6 +22,16 @@
#
+ + + + + - + - + - + - + - + - + - + @@ -187,10 +200,10 @@ found in resources/pocco.html

frompygmentsimportlexers,formattersfromsubprocessimportPopen,PIPE - + - + - + - + - + - + - + - + - + - + @@ -310,50 +337,52 @@ is lib/example.py, the HTML will be at docs/example.htmldef template(source): return lambda context: pystache.render(source, context) - + - + - + - + - +

pocco.py

pocco

+
+
+
+ # +

Pocco is a Python port of Docco: the original quick-and-dirty, hundred-line-long, literate-programming-style documentation generator. It produces HTML that displays your comments @@ -41,20 +51,20 @@ it into a docs folder.

- # + #

Main Documentation Generation Functions

- # + #

Generate the documentation for a source file by reading it in, splitting it up into comment/code sections, highlighting them for the appropriate language, @@ -68,10 +78,10 @@ and merging them into an HTML template.

sections) generate_html(source, sections)
- # + #

Given a string of source code, parse out each comment and the code that follows it, and create an individual section for it. @@ -91,6 +101,9 @@ Sections take the form:

language = get_language(source) has_code = docs_text = code_text = "" + if lines[0].startswith("#!"): + lines.pop(0) + def save(docs, code): sections.append({ "docs_text": docs, @@ -109,10 +122,10 @@ Sections take the form:

save(docs_text, code_text) return sections
- # + #

Highlights a single chunk of code using the Pygments module, and runs the text of its corresponding comment through Markdown.

@@ -135,10 +148,10 @@ wherever our markers occur.

section["docs_html"] = markdown(section["docs_text"]) section["num"] = i
- # + #

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 @@ -160,20 +173,20 @@ found in resources/pocco.html

fh.write(html) fh.close()
- # + #

Helpers & Setup

- # + #

Import our external dependencies.

- # + #

A list of the languages that Pocco supports, mapping the file extension to the name of the Pygments lexer and the symbol that indicates a comment. To @@ -205,30 +218,30 @@ add another language to Pocco's repertoire, add it here.

".scm": { "name": "scheme", "symbol": ";;" }, }
- # + #

Build out the appropriate matchers and delimiters for each language.

for ext, l in languages.items():
- # + #

Does the line begin with a comment?

    l["comment_matcher"] = re.compile(r"^\s*" + l["symbol"] + "\s?")
- # + #

The dividing token we feed into Pygments, to delimit the boundaries between sections.

@@ -236,10 +249,10 @@ sections.

    l["divider_text"] = "\n" + l["symbol"] + "DIVIDER\n"
- # + #

The mirror of divider_text that we expect Pygments to return. We can split on this to recover the original sections.

@@ -247,44 +260,58 @@ on this to recover the original sections.

    l["divider_html"] = re.compile(r'\n*<span class="c[1]?">' + l["symbol"] + 'DIVIDER</span>\n*')
- # + #

Get the Pygments Lexer for this language.

    l["lexer"] = lexers.get_lexer_by_name(l["name"])
- # + #

Get the current language we're documenting, based on the extension.

def get_language(source):
-    return languages[ source[source.rindex("."):] ]
+ try: + return languages[ source[source.rindex("."):] ] + except ValueError: + source = open(source, "r") + code = source.read() + source.close() + lang = lexers.guess_lexer(code).name.lower() + for l in languages.values(): + if l["name"] == lang: + return l + else: + raise ValueError("Can't figure out the language!")
- # + #

Compute the destination HTML path for an input source file path. If the source is lib/example.py, the HTML will be at docs/example.html

def destination(filepath):
-    name = filepath.replace(filepath[ filepath.rindex("."): ], "")
+    try:
+        name = filepath.replace(filepath[ filepath.rindex("."): ], "")
+    except ValueError:
+        name = filepath
     return "docs/" + path.basename(name) + ".html"
- # + #

Shift items off the front of the list until it is empty, then return default.

@@ -296,10 +323,10 @@ is lib/example.py, the HTML will be at docs/example.htmlexcept IndexError: return default
- # + #

Ensure that the destination directory exists.

- # + #

Create the template that we will use to generate the Pocco HTML page.

-
pocco_template = template(open(path.dirname(__file__) + "/resources/pocco.html").read())
+
pocco_template = template(open(path.join(path.dirname(__file__),
+                                         "resources/pocco.html")).read())
- # + #

The CSS styles we"d like to apply to the documentation.

-
pocco_styles = open(path.dirname(__file__) + "/resources/pocco.css").read()
+
pocco_styles = open(path.join(path.dirname(__file__),
+                              "resources/pocco.css")).read()
- # + #

The start of each Pygments highlight block.

highlight_start = "<div class=\"highlight\"><pre>"
- # + #

The end of each Pygments highlight block.

highlight_end = "</pre></div>"
- # + #

Run the script. For each source file passed in as an argument, generate the documentation.

-- 2.43.0