示例#1
0
文件: hmath.py 项目: kmill/htmacros
def inline_math_handler(stream, char_env, escape_env, begin_stack) :
    possible_start_failure = stream.failure("Missing closing '$ for math mode.")
    stream.read()
    if stream.peek() == '$' :
        raise possible_start_failure.with_message("Math mode '$$' is empty.")
    begin_stack2 = begin_stack + ["$"]
    out = parse_one(stream, math_char_env, escape_env, begin_stack2)
    while stream.peek() != '$' :
        if stream.peek() == "" :
            raise possible_start_failure
        else :
            out += parse_one(stream, math_char_env, escape_env, begin_stack2)
    stream.read() # read the final '$'
    return make_render_math(out)
示例#2
0
def multirow_handler(stream, char_env, token_env, begin_stack) :
    numrows = parse_one(stream, char_env, token_env, begin_stack)
    width = parse_one(stream, char_env, token_env, begin_stack)
    try :
        numrows = numrows.eval({})
        width = width.eval({})
    except AttributeError :
        raise stream.failure("Number of rows and width string for multirow must be evaluatable.")
    try :
        numrows = int(numrows.s)
    except :
        raise stream.failure("The number of rows for multirow must be an integer.")
    if type(width) is not StringToken :
        raise stream.failure("Width string must be a string for multirow.")
    return RowSpanToken(numrows, width.s)
示例#3
0
def fileref_handler(stream, char_env, token_env, begin_stack) :
    poss_err = stream.failure()
    barg = read_bracket_args(stream, char_env, token_env, begin_stack)
    filename = parse_one(stream, char_env, token_env, begin_stack)
    
    def _handler(env) :
        fn = filename.eval(env).s # brittle brittle brittle <<< vvv
        if barg is None :
            linktext = os.path.split(fn)[1]
        else :
            linktext = barg.eval(env).s

        fn2 = os.path.abspath(os.path.join(token_env["_global_input_dir"], fn))
        outfile = os.path.abspath(os.path.join(token_env["_curr_out_dir"], fn))
        
        print "Copying",repr(fn2),"to",repr(outfile),
        
        if not os.path.isdir(os.path.split(outfile)[0]) :
            os.makedirs(os.path.split(outfile)[0], 0755)
        
        if os.path.isfile(outfile) and os.path.getmtime(outfile) >= os.path.getmtime(fn2):
            print "... already copied."
        else :
            print
            shutil.copy(fn2, outfile)
        
        relout = os.path.relpath(outfile, token_env["_curr_out_dir"])
        return StringToken("<A HREF=\""+relout+"\">"+linktext+"</A>")
    
    return LambdaToken(_handler)
示例#4
0
def caption_handler(stream, char_env, escape_env, begin_stack) :
    text = parse_one(stream, char_env, escape_env, begin_stack)
    def _handler(env) :
        teval = text.eval(env)
        return (StringToken("<div class=\"caption\"><b>Figure "+counters_to_string("figure")+".</b> ")+teval
                +StringToken("</div>"))
    return LambdaToken(_handler)
示例#5
0
def link_handler(stream, char_env, token_env, begin_stack) :
    poss_err = stream.failure()
    barg = read_bracket_args(stream, char_env, token_env, begin_stack)
    linkurl = parse_one(stream, global_char_env, token_env, begin_stack)
    if barg is None :
        barg = linkurl
    return StringToken("<A HREF=\"") + linkurl + StringToken("\">") + barg + StringToken("</A>")
示例#6
0
def multicolumn_handler(stream, char_env, token_env, begin_stack) :
    numcols = parse_one(stream, char_env, token_env, begin_stack)
    formatting = parse_one(stream, char_env, token_env, begin_stack)
    try :
        numcols = numcols.eval({})
        formatting = formatting.eval({})
    except AttributeError :
        raise stream.failure("Number of columns and formatting string for multicolumn must be evaluatable.")
    try :
        numcols = int(numcols.s)
    except :
        raise stream.failure("The number of columns for multicolumn must be an integer.")
    if type(formatting) is not StringToken :
        raise stream.failure("Formatting string must be a string for multicolumn.")
    f = decode_column_formats(formatting.s)
    if len(f) != 1 :
        raise stream.failure("Multicolumn must have formatting for exactly one column.")
    return ColSpanToken(numcols, f[0])
示例#7
0
def subsection_handler(stream, char_env, token_env, begin_stack) :
    text = parse_one(stream, char_env, token_env, begin_stack)
    def _handler(env) :
        get_counter("subsubsection").increment()
        id = generate_id("subsubsection", get_counter("page"), get_counter("section"), get_counter("subsection"), get_counter("subsubsection"))
        set_anchor_reference(token_env, id, text)
        return (ParagraphToken() + InhibitParagraphToken() + get_anchor_by_id(id) + InhibitParagraphToken()
                + StringToken("<H4>"+counters_to_string("section", "subsection", "subsubsection")+". ") + text + StringToken("</H4>")
                + ParagraphToken())
    return LambdaToken(_handler)
示例#8
0
def emph_handler(stream, char_env, token_env, begin_stack) :
    try :
        in_emph = token_env["_in_emph"]
    except KeyError :
        in_emph = False
    emphed = parse_one(stream, char_env, token_env.extend({"_in_emph" : not in_emph}), begin_stack)
    if in_emph :
        return StringToken("<SPAN CLASS=\"de_em\">") + emphed + StringToken("</SPAN>")
    else :
        return StringToken("<EM>") + emphed + StringToken("</EM>")
示例#9
0
文件: hmath.py 项目: kmill/htmacros
 def _handler(stream, char_handlers, escape_env, begin_stack) :
     if not token :
         stream.read()
     o = MathOpenToken(stream, display_text)
     if stream.peek() == "$" :
         raise stream.failure("No closing delimiter for "+repr(o)+".")
     insides = None
     t = parse_one(stream, char_handlers, escape_env, begin_stack)
     while type(t) != MathCloseToken :
         if stream.peek() == "$" :
             raise stream.failure("No closing delimiter for "+repr(o)+".")
         elif insides == None :
             insides = t
         else :
             insides += t
         t = parse_one(stream, char_handlers, escape_env, begin_stack)
     o.insides = MathBraceStartToken(stream) + insides
     o.close = t
     return o
示例#10
0
def breadcrumb_handler(stream, char_env, token_env, begin_stack) :
    name = read_bracket_args(stream, char_env, token_env, begin_stack)
    label = parse_one(stream, global_char_env, token_env, begin_stack)
    def _handler(env) :
        if "_breadcrumbs" not in token_env["_fluid_let"] :
            token_env["_fluid_let"].append("_breadcrumbs")
            token_env["_breadcrumbs"] = []
        token_env["_breadcrumbs"] = (token_env["_breadcrumbs"]
                                     + [(name if name is None else name.eval(env),label.eval(env))])
        return StringToken("")
    return LambdaToken(_handler)
示例#11
0
def relref_handler(stream, char_env, token_env, begin_stack) :
    poss_err = stream.failure()
    filename = parse_one(stream, char_env, token_env, begin_stack)

    def _handler(env) :
        fn = filename.eval(env).s
        fn2 = os.path.abspath(os.path.join(token_env["_global_base_out_dir"], fn))
        fnrel = os.path.relpath(fn2, token_env["_curr_out_dir"])
        return StringToken(fnrel)

    return LambdaToken(_handler)
示例#12
0
def footnote_handler(stream, char_env, token_env, begin_stack) :
    footnote = parse_one(stream, char_env, token_env, begin_stack)
    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>")
    return LambdaToken(_handler)
示例#13
0
def begin_tabular_environment(stream, char_env, escape_env) :
    def col_break_handler(stream, char_env, escape_env, begin_stack) :
        stream.read()
        return ColumnBreakToken()
    formatting = parse_one(stream, char_env, escape_env, [])
    try :
        formatting = formatting.eval({})
    except AttributeError :
        raise stream.failure("Formatting string for tabular must be evaluatable.")
    if type(formatting) is not StringToken :
        raise stream.failure("Formatting string must be a string.")
    return (char_env.extend({"&" : col_break_handler}),
            escape_env.extend({"_table_formatting" : formatting.s}))
示例#14
0
def set_page_template(stream, char_env, token_env, begin_stack) :
    name = parse_one(stream, char_env, token_env, begin_stack)
    try :
        name = name.eval({})
    except AttributeError :
        raise stream.failure("Name must not be parameterized for set_page_template.")
    if type(name) != StringToken :
        raise stream.failure("Name for \\setpagetemplate must be a string.")
    old = token_env["_global_input_dir"]
    fn = os.path.abspath(os.path.join(token_env["_global_input_dir"], name.s))
    token_env["_global_input_dir"] = os.path.split(fn)[0]
    token_env["_page_template"] = parse_all(fileStream(fn), char_env, token_env, [], execute=False)
    token_env["_global_input_dir"] = old
    return StringToken("")
示例#15
0
def title_handler(stream, char_env, token_env, begin_stack) :
    title = parse_one(stream, char_env, token_env, begin_stack)
    possible_error = stream.failure()
    def _handler(env) :
        if token_env.has_key("_page_title") :
            raise possible_error.with_message("Page has two titles.")
        title2 = title.eval(env)
        token_env["_page_title"] = title2
        if token_env.has_key("_curr_page_reference") :
            token_env["_curr_page_reference"].autoname = title2
        else :
            raise stream.failure("No page for title.")
#        return StringToken("<H1>")+title+StringToken("</H1>") + InhibitParagraphToken()
        return StringToken("")
    return LambdaToken(_handler)
示例#16
0
def begin_page_environment(stream, char_env, escape_env) :
    file = parse_one(stream, char_env, escape_env, [])
    try :
        file = file.eval({})
    except AttributeError :
        raise stream.failure("Page file name must not be parameterized.")
    if type(file) != StringToken :
        raise stream.failure("File name must be a string.")
    
    get_counter("page").increment()
    pageid = generate_id("page", get_counter("page"))
    escape_env["_curr_pageid"] = pageid
    add_counter("footnote")

    set_page_reference(escape_env, pageid, file.s)
    return (char_pretty_text,
            escape_env.extend({"_page_footnotes" : []}))
示例#17
0
def set_stylesheet_handler(stream, char_env, token_env, begin_stack) :
    name = parse_one(stream, char_env, token_env, begin_stack)
    try :
        name = name.eval({})
    except AttributeError :
        raise stream.failure("Name must not be parameterized for setstylesheet.")
    if type(name) != StringToken :
        raise stream.failure("Name for \\setstylesheet must be a string.")
    filename = os.path.abspath(os.path.join(token_env["_global_input_dir"], name.s))
    cssdir = os.path.join(token_env["_global_base_out_dir"], "css")
    if not os.path.isdir(cssdir) :
        os.makedirs(cssdir, 0755)
    filestail = os.path.split(filename)[1]
    destname = os.path.join(cssdir, filestail)
    shutil.copy(filename, destname)
    token_env["_page_css"] = destname
    return StringToken("")
示例#18
0
def label_handler(stream, char_env, token_env, begin_stack) :
    poss_failure = stream.failure()
    name = parse_one(stream, global_char_env, token_env, begin_stack)
    def _handler(env) :
        global _last_object_for_label
        try :
            n = name.eval(env)
        except AttributeError :
            raise poss_failure.with_message("Label name must be able to be evaluated.")
        if type(n) is not StringToken :
            raise poss_failure.with_message("Label must be a string.")
        labelname = n.s
        if _last_object_for_label is None :
            raise poss_failure.with_message("Label "+repr(labelname)+" given for no object.")

        lr = _last_object_for_label.make_link_reference(labelname)
        if type(_last_object_for_label) is PendingPageReference :
            token_env["_curr_page_reference"] = lr
            token_env["_curr_page_label"] = labelname
        _last_object_for_label = False
        _references[lr.get_name()] = lr
        _id_to_reference_name[lr.id] = lr.get_name()
        return InhibitParagraphToken()
    return LambdaToken(_handler)
示例#19
0
文件: images.py 项目: kmill/htmacros
def includegraphics_handler(stream, char_env, token_env, begin_stack) :
    poss_err = stream.failure()
    barg = read_bracket_args(stream, char_env, token_env, begin_stack)
    filename = parse_one(stream, char_env, token_env, begin_stack)
    
    def _handler(env) :
        fn = filename.eval(env).s # brittle brittle brittle <<< vvv
        if barg is None :
            args = ""
        else :
            args = barg.eval(env).s

        fn2 = os.path.abspath(os.path.join(token_env["_global_input_dir"], fn))
        outdir = os.path.abspath(os.path.join(token_env["_curr_out_dir"], os.path.split(fn)[0]))
        args = [[x.strip() for x in y.split("=") if x.strip() != ""] for y in args.split(",")]
        args = [x for x in args if x != []]
#        print args
        args = dict(args)

#        print fn, outdir, args

        dimstring = ""
        altstring = args.get("alt","")
        
        width = args.get("width","")
        height = args.get("height","")
        ext = args.get("ext",None)
        page = args.get("page",None)

        if width == "" and height == "" and ext is None and page is None :
            outfile = os.path.join(outdir, os.path.split(fn)[1])
            if not os.path.isdir(os.path.split(outfile)[0]) :
                os.makedirs(os.path.split(outfile)[0], 0755)
            shutil.copy(fn2, outfile)
        else : # need to convert!
            pagemod = "" if page is None else ("["+str(page)+"]")
            pagemangle = "" if page is None else ("_p"+str(page)+"_")
            
            resize = width
            if height != "" :
                resize += "x"+height
            resize_arg = []
            if resize != "" :
                resize_arg = ["-resize",resize]
            r_mangle = "" if resize == "" else (resize + "_")

            outfile = os.path.join(outdir, r_mangle + pagemangle + os.path.split(fn)[1]) + ("" if ext is None else ("."+ext))
            if not os.path.isdir(os.path.split(outfile)[0]) :
                os.makedirs(os.path.split(outfile)[0], 0755)
            print "Converting",repr(fn2),"to",repr(outfile),
            if os.path.isfile(outfile) and os.path.getmtime(outfile) >= os.path.getmtime(fn2):
                print "... already converted."
            else :
                print ["convert", fn2, resize_arg, outfile]
                retcode = subprocess.call(["convert", fn2+pagemod] + resize_arg + [outfile])
                if retcode != 0 :
                    raise Exception("Retcode was "+str(retcode))
                print "... done"
        relout = os.path.relpath(outfile, token_env["_curr_out_dir"])
        return StringToken("<IMG ALT=\""+altstring+"\""+dimstring+"SRC=\""+relout+"\">")
    return LambdaToken(_handler)
示例#20
0
def ref_handler(stream, char_env, token_env, begin_stack) :
    poss_err = stream.failure()
    barg = read_bracket_args(stream, char_env, token_env, begin_stack)
    labelname = parse_one(stream, global_char_env, token_env, begin_stack)
    return make_reference(token_env, labelname, barg)
示例#21
0
文件: hmath.py 项目: kmill/htmacros
def math_bb_handler(stream, char_env, token_env, begin_stack) :
    charenv2 = char_env.extend({})
    for c,ent in mathbb_dict.iteritems() :
        charenv2[c] = _make_mathord_handler(ent, False)
    return parse_one(stream, charenv2, token_env, begin_stack)
示例#22
0
文件: hmath.py 项目: kmill/htmacros
def math_it_handler(stream, char_env, token_env, begin_stack) :
    charenv2 = char_env.extend({})
    for c in string.ascii_letters :
        charenv2[c] = _make_mathord_handler("<i>"+c+"</i>", False)
    return parse_one(stream, charenv2, token_env, begin_stack)
示例#23
0
def modified_handler(stream, char_env, token_env, begin_stack) :
    modified = parse_one(stream, char_env, token_env, begin_stack)
    def _handler(env) :
        token_env["_page_modified"] = modified
        return StringToken("")
    return LambdaToken(_handler)
示例#24
0
文件: hmath.py 项目: kmill/htmacros
def sup_handler(stream, char_env, escape_env, begin_stack) :
    stream.read()
    t = parse_one(stream, char_env, escape_env, begin_stack)
    return MathSubToken(t)
示例#25
0
文件: hmath.py 项目: kmill/htmacros
def math_frac_handler(stream, char_env, token_env, begin_stack) :
    num = parse_one(stream, char_env, token_env, begin_stack)
    denom = parse_one(stream, char_env, token_env, begin_stack)
    table = StringToken('<TABLE CLASS="mathfrac"><TR CLASS="mathfracnum"><TD>')
    table += num + StringToken("</TD></TR><TR><TD>") + denom + StringToken("</TD></TR></TABLE>")
    return MathPassThroughToken(table)
示例#26
0
文件: hmath.py 项目: kmill/htmacros
def unrendered_math_handler(stream, char_env, escape_env, begin_stack) :
    out = parse_one(stream, math_char_env, escape_env, begin_stack)
    return out