def fix_latex_file(latex_file, pdf_output): # python 2 does not allow to declare a string as raw byte so we double # the backslashes and remove the r preffix def_re = re.compile(b"(\\\\newcommandx|\\\\global\\\\long\\\\def)" b"(\\\\[a-zA-Z]+)") tmp = tempfile.TemporaryFile() changed = False macros = [] for line in open(latex_file, 'rb').readlines(): if not pdf_output and line.startswith(b"\\documentclass"): changed = True line += b"\\PassOptionsToPackage{draft}{microtype}\n" else: match = def_re.match(line) if match != None: macroname = match.group(2) if macroname in macros: definecmd = match.group(1) if definecmd == b"\\newcommandx": changed = True line = line.replace(definecmd, b"\\renewcommandx") else: macros.append(macroname) tmp.write(line) if changed: copyfileobj(tmp, open(latex_file,"wb"), 1) return changed
def fix_latex_file(latex_file, pdf_output): # python 2 does not allow to declare a string as raw byte so we double # the backslashes and remove the r preffix def_re = re.compile(b"(\\\\newcommandx|\\\\global\\\\long\\\\def)" b"(\\\\[a-zA-Z]+)") tmp = tempfile.TemporaryFile() changed = False macros = [] for line in open(latex_file, 'rb').readlines(): if not pdf_output and line.startswith(b"\\documentclass"): changed = True line += b"\\PassOptionsToPackage{draft}{microtype}\n" else: match = def_re.match(line) if match != None: macroname = match.group(2) if macroname in macros: definecmd = match.group(1) if definecmd == b"\\newcommandx": changed = True line = line.replace(definecmd, b"\\renewcommandx") else: macros.append(macroname) tmp.write(line) if changed: copyfileobj(tmp, open(latex_file, "wb"), 1) return changed
def fix_latex_file(latex_file, pdf_output): def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)") tmp = mkstemp() changed = False macros = [] for line in open(latex_file, 'r').readlines(): if not pdf_output and line.startswith("\\documentclass"): changed = True line += "\\PassOptionsToPackage{draft}{microtype}\n" else: match = def_re.match(line) if match != None: macroname = match.group(2) if macroname in macros: definecmd = match.group(1) if definecmd == "\\newcommandx": changed = True line = line.replace(definecmd, "\\renewcommandx") else: macros.append(macroname) tmp.write(line) if changed: copyfileobj(tmp, open(latex_file, "wb"), 1) return changed
def color_pdf(latex_file, bg_color, fg_color): use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})") tmp = mkstemp() fg = "" if fg_color != "0.000000,0.000000,0.000000": fg = ' \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color) success = 0 try: for line in open(latex_file, 'r').readlines(): match = use_preview_pdf_re.match(line) if match == None: tmp.write(line) continue success = 1 tmp.write(" \\usepackage{color}\n" \ " \\pagecolor[rgb]{%s}\n" \ "%s" \ "%s\n" \ % (bg_color, fg, match.group())) continue except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in color_pdf! Unable to open "%s"' % latex_file) warning(`sys.exc_type` + ',' + `sys.exc_value`) if success: copyfileobj(tmp, open(latex_file,"wb"), 1) return success
def fix_latex_file(latex_file, pdf_output): def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)") tmp = mkstemp() changed = False macros = [] for line in open(latex_file, 'r').readlines(): if not pdf_output and line.startswith("\\documentclass"): changed = True line += "\\PassOptionsToPackage{draft}{microtype}\n" else: match = def_re.match(line) if match != None: macroname = match.group(2) if macroname in macros: definecmd = match.group(1) if definecmd == "\\newcommandx": changed = True line = line.replace(definecmd, "\\renewcommandx") else: macros.append(macroname) tmp.write(line) if changed: copyfileobj(tmp, open(latex_file,"wb"), 1) return changed
def crop_files(pnmcrop, basename): t = pipes.Template() t.append('%s -left' % pnmcrop, '--') t.append('%s -right' % pnmcrop, '--') for file in glob.glob("%s*.ppm" % basename): tmp = mkstemp() new = t.open(file, "r") copyfileobj(new, tmp) if not new.close(): copyfileobj(tmp, open(file,"wb"), 1)
def crop_files(pnmcrop, basename): t = pipes.Template() t.append('%s -left' % pnmcrop, '--') t.append('%s -right' % pnmcrop, '--') for file in glob.glob("%s*.ppm" % basename): tmp = tempfile.TemporaryFile() new = t.open(file, "r") copyfileobj(new, tmp) if not new.close(): copyfileobj(tmp, open(file, "wb"), 1)
def legacy_latex_file(latex_file, fg_color, bg_color): use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}") fg_color_gr = make_texcolor(fg_color, True) bg_color_gr = make_texcolor(bg_color, True) tmp = mkstemp() success = 0 try: f = open(latex_file, 'r') except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) warning( ` sys.exc_type ` + ',' + ` sys.exc_value `) for line in f.readlines(): if success: tmp.write(line) continue match = use_preview_re.match(line) if match == None: tmp.write(line) continue success = 1 # Package order: color should be loaded before preview # Preview options: add the options lyx and tightpage tmp.write(r""" \usepackage{color} \definecolor{fg}{rgb}{%s} \definecolor{bg}{rgb}{%s} \pagecolor{bg} \usepackage[%s,tightpage]{preview} \makeatletter \def\t@a{cmr} \if\f@family\t@a \IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}} \fi \g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}} \g@addto@macro\endpreview{\endgroup} \makeatother """ % (fg_color_gr, bg_color_gr, match.group(1))) if success: copyfileobj(tmp, open(latex_file, "wb"), 1) return success
def legacy_latex_file(latex_file, fg_color, bg_color): use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}") fg_color_gr = make_texcolor(fg_color, True) bg_color_gr = make_texcolor(bg_color, True) tmp = mkstemp() success = 0 try: f = open(latex_file, 'r') except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) warning(`sys.exc_type` + ',' + `sys.exc_value`) for line in f.readlines(): if success: tmp.write(line) continue match = use_preview_re.match(line) if match == None: tmp.write(line) continue success = 1 # Package order: color should be loaded before preview # Preview options: add the options lyx and tightpage tmp.write(r""" \usepackage{color} \definecolor{fg}{rgb}{%s} \definecolor{bg}{rgb}{%s} \pagecolor{bg} \usepackage[%s,tightpage]{preview} \makeatletter \def\t@a{cmr} \if\f@family\t@a \IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}} \fi \g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}} \g@addto@macro\endpreview{\endgroup} \makeatother """ % (fg_color_gr, bg_color_gr, match.group(1))) if success: copyfileobj(tmp, open(latex_file,"wb"), 1) return success
def fix_latex_file(latex_file): documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)") tmp = mkstemp() changed = 0 for line in open(latex_file, 'r').readlines(): match = documentclass_re.match(line) if match == None: tmp.write(line) continue changed = 1 tmp.write("%s%s\n" % (match.group(1), match.group(3))) if changed: copyfileobj(tmp, open(latex_file,"wb"), 1) return changed
def legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr): use_preview_dvi_re = re.compile("(\s*\\\\usepackage\[[^]]+)(dvips\]{preview})") use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)(pdftex\]{preview})") tmp = mkstemp() success = 0 try: for line in open(latex_file, 'r').readlines(): match = use_preview_dvi_re.match(line) if match == None: match = use_preview_pdf_re.match(line) if match == None: tmp.write(line) continue success = 1 tmp.write(" \\usepackage{color}\n" \ " \\pagecolor[rgb]{%s}\n" \ "%s\n" \ % (bg_color_gr, match.group())) continue success = 1 tmp.write("%stightpage,%s\n" \ " \\AtBeginDocument{\\AtBeginDvi{%%\n" \ " \\special{!userdict begin/bop-hook{//bop-hook exec\n" \ " <%s%s>{255 div}forall setrgbcolor\n" \ " clippath fill setrgbcolor}bind def end}}}\n" \ % (match.group(1), match.group(2), fg_color, bg_color)) except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) warning(`sys.exc_type` + ',' + `sys.exc_value`) if success: copyfileobj(tmp, open(latex_file,"wb"), 1) return success
def legacy_latex_file(latex_file, fg_color, bg_color): use_polyglossia_re = re.compile(b"\\s*\\\\usepackage{polyglossia}") use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}") fg_color_gr = make_texcolor(fg_color, True) bg_color_gr = make_texcolor(bg_color, True) tmp = tempfile.TemporaryFile() success = 0 try: f = open(latex_file, 'rb') except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1])) polyglossia = False for line in f.readlines(): if success: tmp.write(line) continue match = use_preview_re.match(line) polymatch = use_polyglossia_re.match(line) # Package order: # * if polyglossia is used, we need to load color before that # (also, we do not have to load lmodern) # * else, color should be loaded before preview if match == None: if polymatch == None: tmp.write(line) continue else: tmp.write(b""" \\usepackage{color} \\definecolor{fg}{rgb}{%s} \\definecolor{bg}{rgb}{%s} \\pagecolor{bg} \\usepackage{polyglossia} """ % (fg_color_gr, bg_color_gr)) polyglossia = True continue success = 1 # Preview options: add the options lyx and tightpage previewopts = match.group(1) if not polyglossia: tmp.write(b""" \\usepackage{color} \\definecolor{fg}{rgb}{%s} \\definecolor{bg}{rgb}{%s} \\pagecolor{bg} \\usepackage[%s,tightpage]{preview} \\makeatletter \\def\\t@a{cmr} \\if\\f@family\\t@a \\IfFileExists{lmodern.sty}{\\usepackage{lmodern}}{\\usepackage{ae,aecompl}} \\fi \\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath fill}\\color{fg}} \\g@addto@macro\\endpreview{\\endgroup} \\makeatother """ % (fg_color_gr, bg_color_gr, previewopts)) else: tmp.write(b""" \\usepackage[%s,tightpage]{preview} \\makeatletter \\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath fill}\\color{fg}} \\g@addto@macro\\endpreview{\\endgroup} \\makeatother """ % previewopts) if success: copyfileobj(tmp, open(latex_file,"wb"), 1) return success
def legacy_latex_file(latex_file, fg_color, bg_color): use_polyglossia_re = re.compile(b"\\s*\\\\usepackage{polyglossia}") use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}") fg_color_gr = make_texcolor(fg_color, True) bg_color_gr = make_texcolor(bg_color, True) tmp = tempfile.TemporaryFile() success = 0 try: f = open(latex_file, 'rb') except: # Unable to open the file, but do nothing here because # the calling function will act on the value of 'success'. warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1])) polyglossia = False for line in f.readlines(): if success: tmp.write(line) continue match = use_preview_re.match(line) polymatch = use_polyglossia_re.match(line) # Package order: # * if polyglossia is used, we need to load color before that # (also, we do not have to load lmodern) # * else, color should be loaded before preview if match == None: if polymatch == None: tmp.write(line) continue else: tmp.write(b""" \\usepackage{color} \\definecolor{fg}{rgb}{%s} \\definecolor{bg}{rgb}{%s} \\pagecolor{bg} \\usepackage{polyglossia} """ % (fg_color_gr, bg_color_gr)) polyglossia = True continue success = 1 # Preview options: add the options lyx and tightpage previewopts = match.group(1) if not polyglossia: tmp.write(b""" \\usepackage{color} \\definecolor{fg}{rgb}{%s} \\definecolor{bg}{rgb}{%s} \\pagecolor{bg} \\usepackage[%s,tightpage]{preview} \\makeatletter \\def\\t@a{cmr} \\if\\f@family\\t@a \\IfFileExists{lmodern.sty}{\\usepackage{lmodern}}{\\usepackage{ae,aecompl}} \\fi \\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath fill}\\color{fg}} \\g@addto@macro\\endpreview{\\endgroup} \\makeatother """ % (fg_color_gr, bg_color_gr, previewopts)) else: tmp.write(b""" \\usepackage[%s,tightpage]{preview} \\makeatletter \\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath fill}\\color{fg}} \\g@addto@macro\\endpreview{\\endgroup} \\makeatother """ % previewopts) if success: copyfileobj(tmp, open(latex_file, "wb"), 1) return success