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

easier solution for <<doc>> #22

Open
jedie opened this issue May 15, 2014 · 0 comments
Open

easier solution for <<doc>> #22

jedie opened this issue May 15, 2014 · 0 comments

Comments

@jedie
Copy link
Owner

jedie commented May 15, 2014

Maybe change the <<doc>> code with:

def headlines2html(headlines):
    cur_level = 0
    result = []
    for level, content in headlines:
        while level>cur_level:
            result.append('\t'*cur_level+'<ul>')
            cur_level += 1
        while level<cur_level:
            cur_level -= 1
            result.append('\t'*cur_level+'</ul>')
        result.append('{0}<li><a href="#{1}">{1}</a></li>'.format('\t'*cur_level, content))
    while cur_level:
        cur_level -= 1
        result.append('\t'*cur_level+'</ul>')
    return "\n".join(result)


headlines = [
    (1, u'headline 1 level 1'),
    (2, u'headline 2 level 2'),
    (2, u'headline 3 level 2'),
    (4, u'headline 4 level 4'),
    (1, u'headline 5 level 1'),
    (3, u'headline 6 level 3')
]

print(headlines2html(headlines))

based on code from http://www.python-forum.de/viewtopic.php?p=258144#p258144

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant