Example #1
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)
Example #2
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>")
Example #3
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)
Example #4
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)
Example #5
0
def begin_figure_environment(stream, char_env, escape_env) :
    placement = read_bracket_args(stream, char_env, escape_env, [])
    newenv = escape_env.extend({})
    newenv["_figure_placement"] = placement
    return (char_env, newenv)
Example #6
0
 def item_handler(stream, char_env, escape_env, begin_stack) :
     if escape_env.bindings.has_key("_in_list") : # are we in a list /right now/?
         args = read_bracket_args(stream, char_env, escape_env, begin_stack)
         return ItemToken(stream, args)
     else : raise stream.failure("Item token found at wrong level of itemize or enumerate.")
Example #7
0
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)