diff options
Diffstat (limited to 'pycco')
-rw-r--r-- | pycco/main.py | 36 | ||||
-rw-r--r-- | pycco/tests/__init__.py | 0 | ||||
-rw-r--r-- | pycco/tests/test_pycco.py | 29 |
3 files changed, 25 insertions, 40 deletions
diff --git a/pycco/main.py b/pycco/main.py index 6b8abac..20e5a6b 100644 --- a/pycco/main.py +++ b/pycco/main.py @@ -116,7 +116,7 @@ def parse(code, language): elif multi_line: # Remove leading spaces - if re.match(r' {:d}'.format(len(indent_level), line)): + if re.match(r' {:d}'.format(len(indent_level)), line): docs_text += line[len(indent_level):] + '\n' else: docs_text += line + '\n' @@ -300,7 +300,7 @@ languages = { "multistart": "=begin", "multiend": "=end"}, ".py": {"name": "python", "symbol": "#", - "multistart": '"""', "multiend": '"""' }, + "multistart": '"""', "multiend": '"""'}, ".scm": {"name": "scheme", "symbol": ";;", "multistart": "#|", "multiend": "|#"}, @@ -343,15 +343,21 @@ def get_language(source, code, language=None): else: raise ValueError("Unknown forced language: " + language) - m = re.match(r'.*(\..+)', os.path.basename(source)) + m = re.match(r'.*(\..+)', os.path.basename(source)) if source else None if m and m.group(1) in languages: return languages[m.group(1)] else: - lang = lexers.guess_lexer(code).name.lower() - for l in languages.values(): - if l["name"] == lang: - return l - else: + try: + lang = lexers.guess_lexer(code).name.lower() + for l in languages.values(): + if l["name"] == lang: + return l + else: + raise ValueError() + except ValueError: + # If pygments can't find any lexers, it will raise its own + # subclass of ValueError. We will catch it and raise ours + # for consistency. raise ValueError("Can't figure out the language!") @@ -392,11 +398,19 @@ def shift(list, default): def ensure_directory(directory): - """Ensure that the destination directory exists.""" - + """ + Sanitize directory string and ensure that the destination directory exists. + """ + # Sanitization regexp copied from + # http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python + control_chars = ''.join(map(unichr, range(0, 32) + range(127, 160))) + control_char_re = re.compile(u'[{}]'.format(re.escape(control_chars))) + directory = control_char_re.sub('', directory) if not os.path.isdir(directory): os.makedirs(directory) + return directory + def template(source): return lambda context: pystache.render(source, context) @@ -426,7 +440,7 @@ def process(sources, preserve_paths=True, outdir=None, language=None): # Proceed to generating the documentation. if sources: - ensure_directory(outdir) + outdir = ensure_directory(outdir) css = open(path.join(outdir, "pycco.css"), "w") css.write(pycco_styles) css.close() diff --git a/pycco/tests/__init__.py b/pycco/tests/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/pycco/tests/__init__.py +++ /dev/null diff --git a/pycco/tests/test_pycco.py b/pycco/tests/test_pycco.py deleted file mode 100644 index 43abe36..0000000 --- a/pycco/tests/test_pycco.py +++ /dev/null @@ -1,29 +0,0 @@ -from hypothesis import given -from hypothesis.strategies import lists, text, booleans, integers -import pycco.main as p -import copy - - -@given(lists(text()), text()) -def test_shift(fragments, default): - if fragments == []: - assert p.shift(fragments, default) == default - else: - fragments2 = copy.copy(fragments) - head = p.shift(fragments, default) - assert [head] + fragments == fragments2 - - -@given(text(), booleans(), text(min_size=1)) -def test_destination(filepath, preserve_paths, outdir): - dest = p.destination(filepath, preserve_paths=preserve_paths, outdir=outdir) - assert dest.startswith(outdir) - assert dest.endswith(".html") - - -@given(integers(min_value=0, max_value=12), text()) -def test_parse(n, source): - languages = p.languages - l = languages[languages.keys()[n]] - parsed = p.parse(source, l) - assert [{"code_text", "docs_text"} == set(s.keys()) for s in parsed] |