diff options
author | Benno Rice <benno@jeamland.net> | 2012-06-13 11:55:42 +1000 |
---|---|---|
committer | Benno Rice <benno@jeamland.net> | 2012-06-13 11:55:42 +1000 |
commit | cf540db2d25fe111fee502bf203f5bb192ddac50 (patch) | |
tree | 66e6965c32888207ecfaa261e91b97ae54ed0c13 | |
parent | a73aad95e666174a6fa5130378ff1a7f8ad63dc5 (diff) | |
download | pycco-cf540db2d25fe111fee502bf203f5bb192ddac50.tar.gz |
Move language detection up into generate_documentation so we only do it once.
-rw-r--r-- | pycco/main.py | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/pycco/main.py b/pycco/main.py index f7b942e..e027e60 100644 --- a/pycco/main.py +++ b/pycco/main.py @@ -43,11 +43,12 @@ def generate_documentation(source, outdir=None, preserve_paths=True): if not outdir: raise TypeError("Missing the required 'outdir' keyword argument.") code = open(source, "r").read() - sections = parse(source, code) - highlight(source, sections, preserve_paths=preserve_paths, outdir=outdir) + language = get_language(source, code) + sections = parse(source, code, language) + highlight(source, sections, language, preserve_paths=preserve_paths, outdir=outdir) return generate_html(source, sections, preserve_paths=preserve_paths, outdir=outdir) -def parse(source, code): +def parse(source, code, language): """ Given a string of source code, parse out each comment and the code that follows it, and create an individual **section** for it. @@ -63,7 +64,6 @@ def parse(source, code): lines = code.split("\n") sections = [] - language = get_language(source) has_code = docs_text = code_text = "" if lines[0].startswith("#!"): @@ -189,7 +189,7 @@ def preprocess(comment, section_nr, preserve_paths=True, outdir=None): # === Highlighting the source code === -def highlight(source, sections, preserve_paths=True, outdir=None): +def highlight(source, sections, language, preserve_paths=True, outdir=None): """ Highlights a single chunk of code using the **Pygments** module, and runs the text of its corresponding comment through **Markdown**. @@ -201,7 +201,6 @@ def highlight(source, sections, preserve_paths=True, outdir=None): if not outdir: raise TypeError("Missing the required 'outdir' keyword argument.") - language = get_language(source) output = pygments.highlight(language["divider_text"].join(section["code_text"].rstrip() for section in sections), language["lexer"], @@ -321,16 +320,13 @@ for ext, l in languages.items(): # Get the Pygments Lexer for this language. l["lexer"] = lexers.get_lexer_by_name(l["name"]) -def get_language(source): +def get_language(source, code): """Get the current language we're documenting, based on the extension.""" m = re.match(r'.*(\..+)', os.path.basename(source)) if m and m.group(1) in languages: return languages[m.group(1)] else: - 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: |