Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to pathlib (py3 stdlib module) #122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions pycco/generate_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
all documentation files generated by Pycco.
"""
import re
from os import path

# from os import path
from pathlib import Path

from pycco.compat import compat_items
from pycco_resources import pycco_template


__all__ = ('generate_index',)
__all__ = ("generate_index",)


def build_tree(file_paths, outdir):
tree = {}
for file_path in file_paths:
entry = {
'path': file_path,
'relpath': path.relpath(file_path, outdir)
}
path_steps = entry['relpath'].split(path.sep)
entry = {"path": file_path, "relpath": Path(file_path).relative_to(outdir)}
path_steps = entry["relpath"].parts
add_file(entry, path_steps, tree)

return tree
Expand All @@ -39,7 +38,7 @@ def add_file(entry, path_steps, tree):
add_file(entry, subpath, tree[node])

else:
tree[node]['entry'] = entry
tree[node]["entry"] = entry


def generate_tree_html(tree):
Expand All @@ -49,16 +48,18 @@ def generate_tree_html(tree):
"""
items = []
for node, subtree in sorted(compat_items(tree)):
if 'entry' in subtree:
html = u'<li><a href="{}">{}</a></li>'.format(subtree['entry']['relpath'], node)
if "entry" in subtree:
html = u'<li><a href="{}">{}</a></li>'.format(
subtree["entry"]["relpath"], node
)
else:
html = u'<dl><dt>{}</dt><dd><ul>{}</ul></dd></dl>'.format(
html = u"<dl><dt>{}</dt><dd><ul>{}</ul></dd></dl>".format(
node, generate_tree_html(subtree)
)

items.append(html)

return ''.join(items)
return "".join(items)


def generate_index(files, outdir):
Expand All @@ -68,11 +69,13 @@ def generate_index(files, outdir):
"""
tree = build_tree(files, outdir)

rendered = pycco_template({
"title": 'Index',
"stylesheet": 'pycco.css',
"sections": {'docs_html': generate_tree_html(tree)},
"source": '',
})
rendered = pycco_template(
{
"title": "Index",
"stylesheet": "pycco.css",
"sections": {"docs_html": generate_tree_html(tree)},
"source": "",
}
)

return re.sub(r"__DOUBLE_OPEN_STACHE__", "{{", rendered).encode("utf-8")
29 changes: 3 additions & 26 deletions pycco/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
DASH_DASH = "--"
TRIPLE_QUOTE = '"""'


def lang(name, comment_symbol, multistart=None, multiend=None):
"""
Generate a language entry dictionary, given a name and comment symbol and
optional start/end strings for multiline comments.
"""
result = {
"name": name,
"comment_symbol": comment_symbol
}
result = {"name": name, "comment_symbol": comment_symbol}
if multistart is not None and multiend is not None:
result.update(multistart=multistart, multiend=multiend)
return result
Expand All @@ -31,46 +29,25 @@ def lang(name, comment_symbol, multistart=None, multiend=None):

supported_languages = {
".coffee": lang("coffee-script", HASH, "###", "###"),

".pl": lang("perl", HASH),

".sql": lang("sql", DASH_DASH, SLASH_STAR, STAR_SLASH),

".sh": lang("bash", HASH),

".c": c_lang,

".h": c_lang,

".cl": c_lang,

".cpp": lang("cpp", SLASH_SLASH),

".js": lang("javascript", SLASH_SLASH, SLASH_STAR, STAR_SLASH),

".rb": lang("ruby", HASH, "=begin", "=end"),

".py": lang("python", HASH, TRIPLE_QUOTE, TRIPLE_QUOTE),

".pyx": lang("cython", HASH, TRIPLE_QUOTE, TRIPLE_QUOTE),

".scm": lang("scheme", ";;", "#|", "|#"),

".lua": lang("lua", DASH_DASH, "--[[", "--]]"),

".erl": lang("erlang", "%%"),

".tcl": lang("tcl", HASH),

".hs": lang("haskell", DASH_DASH, "{-", "-}"),

".r": lang("r", HASH),
".R": lang("r", HASH),

".jl": lang("julia", HASH, "#=", "=#"),

".m": lang("matlab", "%", "%{", "%}"),

".do": lang("stata", SLASH_SLASH, SLASH_STAR, STAR_SLASH)

".do": lang("stata", SLASH_SLASH, SLASH_STAR, STAR_SLASH),
}
Loading