Пример #1
0
 def _handler(env) :
     if not token_env.has_key("_page_footnotes") :
         raise stream.failure("Footnotes can only appear within a page")
     get_counter("footnote").increment()
     id = generate_id("footnote", get_counter("page"), get_counter("footnote"))
     make_label(token_env, "ref_"+id, "ref_"+id, "^")
     token_env["_page_footnotes"].append((id, footnote))
     return get_anchor_by_id("ref_"+id) + StringToken("<sup>") \
         + make_reference(token_env, StringToken("#" + id), None) \
         + StringToken("</sup>")
Пример #2
0
 def eval_footnotes(footnotes) :
     oldlenfoot = 0
     lenfoot = 0
     fout = StringToken("")
     while len(footnotes) > lenfoot :
         oldlenfoot = lenfoot
         lenfoot = len(footnotes)
         out = StringToken("")
         for (num, (id, footnote)) in zip(xrange(oldlenfoot+1,lenfoot+1), footnotes[oldlenfoot:lenfoot]) :
             make_label(token_env, id, id, StringToken("[%d]" % num))
             
             out += HorizontalLineToken() + InhibitParagraphToken() + get_anchor_by_id(id) \
                 + InhibitParagraphToken() \
                 + StringToken("<div class=\"footnote\">\n<sup>") \
                 + make_reference(token_env, StringToken("#ref_" + id), StringToken("[%d]" % num)) \
                 + StringToken("</sup> ") \
                 + footnote \
                 + StringToken("</div>\n")
         fout += out.eval({})
     return fout
Пример #3
0
def end_page_environment(char_env, token_env, outer_token_env, out) :
    def eval_footnotes(footnotes) :
        oldlenfoot = 0
        lenfoot = 0
        fout = StringToken("")
        while len(footnotes) > lenfoot :
            oldlenfoot = lenfoot
            lenfoot = len(footnotes)
            out = StringToken("")
            for (num, (id, footnote)) in zip(xrange(oldlenfoot+1,lenfoot+1), footnotes[oldlenfoot:lenfoot]) :
                make_label(token_env, id, id, StringToken("[%d]" % num))
                
                out += HorizontalLineToken() + InhibitParagraphToken() + get_anchor_by_id(id) \
                    + InhibitParagraphToken() \
                    + StringToken("<div class=\"footnote\">\n<sup>") \
                    + make_reference(token_env, StringToken("#ref_" + id), StringToken("[%d]" % num)) \
                    + StringToken("</sup> ") \
                    + footnote \
                    + StringToken("</div>\n")
            fout += out.eval({})
        return fout
    if token_env.has_key("_page_template") :
        out2 = out.eval({})
        if token_env["_page_footnotes"] :
            out2 += eval_footnotes(token_env["_page_footnotes"])
        rout = render_paragraphing(InhibitParagraphToken()+out2)
        if False : # set to True to debug render_paragraphing
            print "out =",out
            print
            print "rout =",rout
            print
        pageref = token_env["_curr_page_reference"]
        if not os.path.isdir(pageref.dir) :
            os.makedirs(pageref.dir, 0755)
        pagepath = pageref.path()
        if pagepath in _page_filenames :
            raise Exception("Two pages have the same filename "+repr(pagepath))
        _page_filenames.append(pagepath)
        relpagepath = os.path.relpath(pagepath, token_env["_global_base_out_dir"])
        css = StringToken("")
        if token_env.has_key("_page_css") :
            css = StringToken("<LINK REL=\"stylesheet\" HREF=\""
                              +os.path.relpath(token_env["_page_css"], os.path.split(pagepath)[0])
                              +"\" TYPE=\"text/css\">")
        if not token_env.has_key("_page_modified") :
            raise Exception("Page "+repr(pagepath)+" must have \\modified token.")
        # breadcrumbs:
        mybc = [(a[1], a[0], make_reference(token_env, a[1], a[0])) for a in token_env.get("_breadcrumbs", [])]
        def _get_name(overridename, label) :
            print "_getname", overridename, label
            if overridename is not None :
                return overridename
            else :
                return get_autoname_by_ref_name(label)
        if len(mybc) == 0 :
            breadcrumbs = pageref.autoname
        else :
            if mybc[0][0].s == pageref.labelname :
                breadcrumbs = _get_name(mybc[0][1], mybc[0][0])
            else :
                breadcrumbs = mybc[0][2]
            for crumb in mybc[1:] :
                breadcrumbs += StringToken(" > ")
                if crumb[0].s == pageref.labelname :
                    breadcrumbs += _get_name(crumb[1], crumb[0].s)
                else :
                    breadcrumbs += crumb[2]
            if token_env["_breadcrumbs"][-1][1].s != pageref.labelname :
                breadcrumbs += StringToken(" > ") + pageref.autoname
        # make page now
        pagetoken = token_env["_page_template"].eval({"pagetitle": token_env["_page_title"],
                                                      "pagecontent" : rout,
                                                      "pagepath" : StringToken(pagepath),
                                                      "relpagepath" : StringToken(relpagepath),
                                                      "css" : css,
                                                      "pagemodified" : token_env["_page_modified"],
                                                      "breadcrumbs" : breadcrumbs.eval({})})
        if type(pagetoken) == StringToken :
            f = open(pagepath, "w")
            f.write(pagetoken.s)
            f.close()
            print "Wrote page",pagepath
            return StringToken("")
        else :
            print "Token which caused error: ",pagetoken
            raise Exception("Page not a single string.")
    else :
        raise Exception("No page template has been specified to make pages.")