def apply(self, handler, gen): map = { "authors": handler.gen_authors, "content": handler.gen_content, "header": handler.gen_header, "title": handler.gen_title, "toc": handler.gen_menu } global template_re try: tpl = open(self.path, "r") n = 0 for line in tpl.xreadlines(): n = n + 1 f = 0 for m in template_re.finditer(line): gen.out.write(line[f:m.start()]) f = m.end() try: kw = m.group(1) map[kw](gen) except KeyError, e: common.onError("unknown element %s at %d" % (kw, n)) gen.out.write(line[f:]) except IOError, e: common.onError(str(e))
def parse(self, file, name='<unknown>'): try: self.parseInternal(file, name) self.send(doc.Event(doc.L_DOC, doc.ID_END)) self.doc.clean() except common.ParseException, e: common.onError(self.message(e))
def parse(self, file, name = '<unknown>'): try: self.parseInternal(file, name) self.send(doc.Event(doc.L_DOC, doc.ID_END)) self.doc.clean() except common.ParseException, e: common.onError(self.message(e))
def genImage(self, url, width = None, height = None, caption = None, align = None, node = None): # !!TODO!! # It should download the image if the URL is external # handle unsupported image format root, ext = os.path.splitext(url) if ext.lower() not in UNSUPPORTED_IMAGE: link = self.loadFriendFile(url) else: original = self.relocateFriendFile(url) root, ext = os.path.splitext(original) link = self.addFriendFile(os.path.abspath(root + ".png")) res = subprocess.call(['convert %s %s' % (original, link)], shell = True) if res <> 0: common.onError('cannot convert image "%s" to "%s"' % (original, link)) link = self.getFriendRelativePath(link) # build the command args = '' if width: if args: args += ',' args += 'width=%dpx' % width if height: if args: args += ',' args += 'height=%dpx' % height if args: args = "[%s]" % args if align <> doc.ALIGN_NONE: self.out.write("\\begin{figure}[htbp]\n") self.out.write('\includegraphics%s{%s}' % (args, link)) if align <> doc.ALIGN_NONE: self.genLabel(node) self.out.write("\n\\end{figure}\n")
def use(self, name): """Use a module in the current parser.""" if name in self.used_mods: return path = self.doc.getVar("THOT_USE_PATH") mod = common.loadModule(name, path) if mod: self.used_mods.append(mod) mod.init(self) # new syntax? if mod.__dict__.has_key("__syntax__"): lines = [] words = [] if mod.__dict__.has_key("__lines__"): lines = mod.__lines__ if mod.__dict__.has_key("__words__"): words = mod.__words__ self.setSyntax( [(l[0], re.compile(l[1])) for l in lines], [(w[0], w[1]) for w in words]) # simple extension else: if mod.__dict__.has_key("__lines__"): for line in mod.__lines__: self.addLine((line[0], re.compile(line[1]))) if mod.__dict__.has_key("__words__"): for word in mod.__words__: self.addWord((word[0], word[1])) else: common.onError('cannot load module %s' % name)
def use(self, name): """Use a module in the current parser.""" if name in self.used_mods: return path = self.doc.getVar("THOT_USE_PATH") mod = common.loadModule(name, path) if mod: self.used_mods.append(mod) mod.init(self) # new syntax? if mod.__dict__.has_key("__syntax__"): lines = [] words = [] if mod.__dict__.has_key("__lines__"): lines = mod.__lines__ if mod.__dict__.has_key("__words__"): words = mod.__words__ self.setSyntax([(l[0], re.compile(l[1])) for l in lines], [(w[0], w[1]) for w in words]) # simple extension else: if mod.__dict__.has_key("__lines__"): for line in mod.__lines__: self.addLine((line[0], re.compile(line[1]))) if mod.__dict__.has_key("__words__"): for word in mod.__words__: self.addWord((word[0], word[1])) else: common.onError('cannot load module %s' % name)
def handleInclude(man, match): path = match.group(1).strip() if not os.path.isabs(path): path = os.path.join(os.path.dirname(man.file_name), path) try: file = open(path) man.parseInternal(file, path) except IOError, e: common.onError('%s:%d: cannot include "%s": %s' % (man.file_name, man.line_num, path, e))
def copy_friend(self, spath, tpath): """Load a friend file in the generation location. path -- relative path to write to. base -- absolute file to the file to copy.""" tpath = self.prepare_friend(tpath) try: shutil.copyfile(spath, tpath) return tpath except shutil.Error, e: common.onError('can not copy "%s" to "%s": %s' % (spath, tpath, str(e)))
def prepare_friend(self, path): """Prepare friend file to be created (ensuring uniqueness and existence of directories (maintain the same path suffix). Return the actual path.""" # create directories dpath = os.path.dirname(path) if not os.path.exists(dpath): try: os.makedirs(dpath) except os.error, e: common.onError('cannot create directory "%s": %s' % (dpath, e))
def handleTerm(man, match): # record the term id = match.group("termid") de = match.group("termdef") if man.lexicon.exists(id): common.onError(man.message("term \"%s\" already defined!" % id)) return term = LexPar(id) man.lexicon.add(id, term) # finalize the parsing man.doc.addLabel(label(id), term) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, term)) man.reparse(de)
def __init__(self, doc): back.Generator.__init__(self, doc) self.output = self.doc.getVar('OUTPUT') if self.output: if self.output == 'pdf': self.backend = self.doc.getVar('DOCBOOK_BACKEND') if not self.backend: self.backend = 'dblatex' if self.backend == 'openjade': self.sgml = True elif self.backend == 'dblatex': self.sgml = False else: common.onError('DocBook back-end %s not supported' % self.backend) else: common.onError('DocBook output %s not supported' % self.output)
def genImage(self, url, width=None, height=None, caption=None, align=None, node=None): # !!TODO!! # It should download the image if the URL is external # handle unsupported image format root, ext = os.path.splitext(url) if ext.lower() not in UNSUPPORTED_IMAGE: link = self.loadFriendFile(url) else: original = self.relocateFriendFile(url) root, ext = os.path.splitext(original) link = self.addFriendFile(os.path.abspath(root + ".png")) res = subprocess.call(['convert %s %s' % (original, link)], shell=True) if res <> 0: common.onError('cannot convert image "%s" to "%s"' % (original, link)) link = self.getFriendRelativePath(link) # build the command args = '' if width: if args: args += ',' args += 'width=%dpx' % width if height: if args: args += ',' args += 'height=%dpx' % height if args: args = "[%s]" % args if align <> doc.ALIGN_NONE: self.out.write("\\begin{figure}[htbp]\n") self.out.write('\includegraphics%s{%s}' % (args, link)) if align <> doc.ALIGN_NONE: self.genLabel(node) self.out.write("\n\\end{figure}\n")
def get_css(self): """Get the CSS style from the current configuration. Return (CSS base, CSS path) or None""" # CSS already computed? if self.css: return self.css # get STYLE style = self.doc.getVar("STYLE") if not style: return (None, None) # absolute path if os.path.isabs(style): if os.path.isdir(style): self.css = (style, "style.css") else: self.css = (os.path.dirname(style), os.path.basename(style)) # in the current directory elif os.path.exists(style): if os.path.isdir(style): self.css = (style, "style.css") else: self.css = (os.path.dirname(style), os.path.basename(style)) # look in the THOT directory else: base = os.path.join(self.doc.getVar("THOT_BASE"), "slidy") path = os.path.join(base, style) if os.path.isdir(path): self.css = (path, "style.css") else: path = os.path.join(base, "styles", style + ".css") if os.path.exists(path): self.css = (base, os.path.join("styles", style + ".css")) else: common.onError("cannot find style '%s'" % style) # return result return self.css
def run(self): # select the page self.template = self.doc.getVar('HTML_TEMPLATE') if self.template: page = TemplatePage(self.template) else: page = PlainPage() # select the policy self.struct = self.doc.getVar('HTML_ONE_FILE_PER') if self.struct == 'document' or self.struct == '': policy = AllInOne(self, page) elif self.struct == 'chapter': policy = PerChapter(self, page) elif self.struct == 'section': policy = PerSection(self, page) else: common.onError('one_file_per %s structure is not supported' % self.struct) # generate the document policy.run() print "SUCCESS: result in %s" % self.path
def run(self): try: # prepare the files to process (base, css) = self.get_css() tpath = self.get_template() tool_base = os.path.join(self.doc.getVar("THOT_BASE"), "slidy") tool_css = os.path.join("styles", "slidy.css") # add CSS self.openMain(".html") tool_rel_css = self.importCSS(tool_css, tool_base) if css: self.rel_css = self.importCSS(css, base) # add scripts ipath = self.getImportDir() spath = os.path.join(tool_base, "scripts") path = os.path.join(ipath, "scripts") if not os.path.exists(path): shutil.copytree(spath, path) # write the output env = dict(self.doc.env) env["IMPORTS"] = ipath env["IF_IMPORTED_STYLE"] = self.gen_imported_style env["SLIDES"] = self.gen_slides env["IF_COVER_IMAGE"] = self.gen_cover_image env["IF_DURATION"] = self.gen_duration env["IF_DOC_LOGO"] = self.gen_doc_logo env["IF_ORG_LOGO"] = self.gen_org_logo templater = Templater(env) templater.gen(tpath, self.out) except IOError as e: common.onError("error during generation: %s" % e)
def unsupported(self, feature): common.onError('%s unsupported for Latex back-end')
def onError(self, msg): """Called to display an error.""" common.onError('%s:%d: %s' % (self.file, self.line, msg))
def error(self, msg): """Display an error with file and line.""" common.onError(self.message(msg))
env["THOT_OUT_PATH"] = options.out_path if args == []: input = sys.__stdin__ env["THOT_FILE"] = "<stdin>" env["THOT_DOC_DIR"] = "." else: input = file(args[0]) env["THOT_FILE"] = args[0] env["THOT_DOC_DIR"] = os.path.dirname(args[0]) if not env["THOT_DOC_DIR"]: env["THOT_DOC_DIR"] = "." if options.defines: for d in options.defines: p = d.find('=') if p == -1: common.onError('-D' + d + ' must follow syntax -Didentifier=value') else: env[d[:p]] = d[p + 1:] # open the output document = doc.Document(env) out_name = env["THOT_OUT_TYPE"] out_path = os.path.join(document.env["THOT_BASE"], "backs") out_driver = common.loadModule(out_name, out_path) if not out_driver: common.onError('cannot find %s back-end' % out_name) # Parse the file man = tparser.Manager(document) if out_driver.__dict__.has_key("init"): out_driver.init(man)
env["THOT_OUT_PATH"] = options.out_path if args == []: input = sys.__stdin__ env["THOT_FILE"] = "<stdin>" env["THOT_DOC_DIR"] = "." else: input = file(args[0]) env["THOT_FILE"] = args[0] env["THOT_DOC_DIR"] = os.path.dirname(args[0]) if not env["THOT_DOC_DIR"]: env["THOT_DOC_DIR"] = "." if options.defines: for d in options.defines: p = d.find('=') if p == -1: common.onError('-D' + d + ' must follow syntax -Didentifier=value') else: env[d[:p]] = d[p+1:] # open the output document = doc.Document(env) out_name = env["THOT_OUT_TYPE"] out_path = os.path.join(document.env["THOT_BASE"], "backs") out_driver = common.loadModule(out_name, out_path) if not out_driver: common.onError('cannot find %s back-end' % out_name) # Parse the file man = tparser.Manager(document) if out_driver.__dict__.has_key("init"): out_driver.init(man)
def run(self): self.openMain('.tex') self.doc.pregen(self) # get class cls = self.doc.getVar('LATEX_CLASS') if not cls: cls = 'book' preamble = self.doc.getVar('LATEX_PREAMBLE') # look for internationalization lang = self.doc.getVar('LANG').lower().replace('-', '_') if lang: if LANGUAGES.has_key(lang): lang = LANGUAGES[lang] else: pos = lang.find('_') if pos < 0: common.onError('cannot not support "%s"' % lang) else: lang = lang[:pos] if LANGUAGES.has_key(lang): lang = LANGUAGES[lang] else: common.onError('cannot not support "%s"' % lang) # look for encoding self.encoding = self.doc.getVar('ENCODING').lower().replace('-', '_') if self.encoding: if self.encoding == 'utf_8': preamble += '\\usepackage{ucs}\n' preamble += '\\usepackage[utf8x]{inputenc}\n' self.encoder = UTF8Encoder() elif self.encoding == 'iso_8859_1': preamble += '\\usepackage[latin1]{inputenc}\n' self.encoder = NonUnicodeEncoder(self.encoding) else: common.onWarning('%s encoding is just ignored for latex' % self.encoding) # look paper size paper = self.doc.getVar('LATEX_PAPER') if not paper: paper = 'a4paper' # preamble self.out.write('\\documentclass[oneside,%s,%s]{%s}\n' % (paper, lang, cls)) self.out.write('\\usepackage[T1]{fontenc}\n') self.out.write('\\usepackage[pdftex]{graphicx}\n') self.out.write('\\usepackage{hyperref}\n') self.out.write('\\usepackage{verbatim}\n') #self.out.write('\\usepackage{fancyhdr}\n') self.out.write(preamble) if lang: self.out.write('\\usepackage[%s]{babel}\n' % lang) is_koma = cls in KOMA_STYLES # add custom definitions self.out.write('\\newcommand{\\superscript}[1]{\\ensuremath{^{\\textrm{#1}}}}\n') self.out.write('\\newcommand{\\subscript}[1]{\\ensuremath{_{\\textrm{#1}}}}\n') self.out.write('\\headheight=20pt\n') self.out.write('\\headsep=10pt\n') self.out.write('\\begin{document}\n') # write title latex_title = self.doc.getVar("LATEX_TITLE") if not latex_title: self.out.write('\\title{%s}\n' % self.escape(self.doc.getVar('TITLE'))) subtitle = self.doc.getVar("SUBTITLE") if is_koma and subtitle: self.out.write("\\subtitle{%s}\n" % self.escape(subtitle)) # NOTE: \thanks{...} allows to give the owner organization of an author self.out.write('\\author{%s}\n' % self.escape(self.doc.getVar('AUTHORS')).replace(",", " \\and ")) organization = self.doc.getVar("ORGANIZATION") if is_koma and organization: self.out.write("\\publishers{%s}\n" % self.escape(organization)) self.out.write('\\maketitle\n\n') else: self.out.write("\\begin{titlepage}\n") self.out.write("\\newcommand{\\thotorganization}{%s}\n" % self.escape(self.doc.getVar("ORGANIZATION"))) self.out.write("\\newcommand{\\thottitle}{%s}\n" % self.escape(self.doc.getVar("TITLE"))) self.out.write("\\newcommand{\\thotsubtitle}{%s}\n" % self.escape(self.doc.getVar("SUBTITLE"))) self.out.write("\\newcommand{\\thotauthors}{%s}\n" % ("{" + self.escape(self.doc.getVar("AUTHORS")).replace(",", "} {") + "}")) logos = self.doc.getVar("LOGO") text = "" fst = True for logo in logos.split(","): if not fst: text = text + " \\hfill "; else: fst = False text = text + "\includegraphics{%s}" % logo.strip() self.out.write("\\newcommand{\\thotlogos}{%s}\n" % text) file = open(latex_title) for l in file: self.out.write(l) self.out.write("\\end{titlepage}\n") # generate the content self.out.write('\\tableofcontents\n\n') self.out.write('\\pagebreak\n\n') # write body self.doc.gen(self) # write footer self.out.write('\\end{document}\n') self.out.close() # generate final format output = self.doc.getVar('OUTPUT') if not output or output == 'latex': print "SUCCESS: result in %s" % self.path elif output == 'pdf': # perform compilation for i in xrange(0, 2): # two times for TOC (sorry) dir, file = os.path.split(self.path) cmd = 'pdflatex -halt-on-error %s' % file if dir == "": dir = "." process = subprocess.Popen( cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE, cwd = dir ) out, err = process.communicate('') if process.returncode <> 0: sys.stdout.write(out) sys.stderr.write(err) return # display result file, ext = os.path.splitext(self.path) if ext == ".tex": path = file else: path = self.path path = path + ".pdf" print "SUCCESS: result in %s" % path else: common.onError('unknown output: %s' % output)
self.to_files[fpath] = "" return fpath def copy_friend(self, spath, tpath): """Load a friend file in the generation location. path -- relative path to write to. base -- absolute file to the file to copy.""" tpath = self.prepare_friend(tpath) try: shutil.copyfile(spath, tpath) return tpath except shutil.Error, e: common.onError('can not copy "%s" to "%s": %s' % (spath, tpath, str(e))) except IOError, e: common.onError('can not copy "%s" to "%s": %s' % (spath, tpath, str(e))) def use_friend(self, path, base=''): """Ensure that a friend is available. If not, get it from the given path. If absolute, the file is loaded in the import directory and a relative path (to the document directory) is returned. path -- path to the file base -- base directory containing the file ('' for CWD files)""" # already declared? tpath = self.get_friend(path, base) if tpath: return tpath # make target path
def finalize_output(self, gen): try: shutil.move(self.out_path, self.get_path(gen)) except IOError as e: common.onError("plantuml error: %s" % e)
# along with this program. If not, see <http://www.gnu.org/licenses/>. import os import os.path import back import common import cgi ZIP = common.Command("zip", "unzip unavaible to build the final archive", common.ERROR_FAIL) # get the html module out_path = os.path.dirname(__file__) html = common.loadModule("html", out_path) if not html: common.onError("cannot found html backend !") # SCORM generator class ScormGenerator(html.Generator): def __init__(self, doc): html.Generator.__init__(self, doc) def genTitle(self): """No title.""" pass def genContent(self, max=7, out=False): """No content.""" pass
def run(self): self.openMain('.tex') self.doc.pregen(self) # get class cls = self.doc.getVar('LATEX_CLASS') if not cls: cls = 'book' preamble = self.doc.getVar('LATEX_PREAMBLE') # look for internationalization lang = self.doc.getVar('LANG').lower().replace('-', '_') if lang: if LANGUAGES.has_key(lang): lang = LANGUAGES[lang] else: pos = lang.find('_') if pos < 0: common.onError('cannot not support "%s"' % lang) else: lang = lang[:pos] if LANGUAGES.has_key(lang): lang = LANGUAGES[lang] else: common.onError('cannot not support "%s"' % lang) # look for encoding self.encoding = self.doc.getVar('ENCODING').lower().replace('-', '_') if self.encoding: if self.encoding == 'utf_8': preamble += '\\usepackage{ucs}\n' preamble += '\\usepackage[utf8x]{inputenc}\n' self.encoder = UTF8Encoder() elif self.encoding == 'iso_8859_1': preamble += '\\usepackage[latin1]{inputenc}\n' self.encoder = NonUnicodeEncoder(self.encoding) else: common.onWarning('%s encoding is just ignored for latex' % self.encoding) # look paper size paper = self.doc.getVar('LATEX_PAPER') if not paper: paper = 'a4paper' # preamble self.out.write('\\documentclass[oneside,%s,%s]{%s}\n' % (paper, lang, cls)) self.out.write('\\usepackage[T1]{fontenc}\n') self.out.write('\\usepackage[pdftex]{graphicx}\n') self.out.write('\\usepackage{hyperref}\n') self.out.write('\\usepackage{verbatim}\n') #self.out.write('\\usepackage{fancyhdr}\n') self.out.write(preamble) if lang: self.out.write('\\usepackage[%s]{babel}\n' % lang) is_koma = cls in KOMA_STYLES # add custom definitions self.out.write( '\\newcommand{\\superscript}[1]{\\ensuremath{^{\\textrm{#1}}}}\n') self.out.write( '\\newcommand{\\subscript}[1]{\\ensuremath{_{\\textrm{#1}}}}\n') self.out.write('\\headheight=20pt\n') self.out.write('\\headsep=10pt\n') self.out.write('\\begin{document}\n') # write title latex_title = self.doc.getVar("LATEX_TITLE") if not latex_title: self.out.write('\\title{%s}\n' % self.escape(self.doc.getVar('TITLE'))) subtitle = self.doc.getVar("SUBTITLE") if is_koma and subtitle: self.out.write("\\subtitle{%s}\n" % self.escape(subtitle)) # NOTE: \thanks{...} allows to give the owner organization of an author self.out.write('\\author{%s}\n' % self.escape( self.doc.getVar('AUTHORS')).replace(",", " \\and ")) organization = self.doc.getVar("ORGANIZATION") if is_koma and organization: self.out.write("\\publishers{%s}\n" % self.escape(organization)) self.out.write('\\maketitle\n\n') else: self.out.write("\\begin{titlepage}\n") self.out.write("\\newcommand{\\thotorganization}{%s}\n" % self.escape(self.doc.getVar("ORGANIZATION"))) self.out.write("\\newcommand{\\thottitle}{%s}\n" % self.escape(self.doc.getVar("TITLE"))) self.out.write("\\newcommand{\\thotsubtitle}{%s}\n" % self.escape(self.doc.getVar("SUBTITLE"))) self.out.write( "\\newcommand{\\thotauthors}{%s}\n" % ("{" + self.escape( self.doc.getVar("AUTHORS")).replace(",", "} {") + "}")) logos = self.doc.getVar("LOGO") text = "" fst = True for logo in logos.split(","): if not fst: text = text + " \\hfill " else: fst = False text = text + "\includegraphics{%s}" % logo.strip() self.out.write("\\newcommand{\\thotlogos}{%s}\n" % text) file = open(latex_title) for l in file: self.out.write(l) self.out.write("\\end{titlepage}\n") # generate the content self.out.write('\\tableofcontents\n\n') self.out.write('\\pagebreak\n\n') # write body self.doc.gen(self) # write footer self.out.write('\\end{document}\n') self.out.close() # generate final format output = self.doc.getVar('OUTPUT') if not output or output == 'latex': print "SUCCESS: result in %s" % self.path elif output == 'pdf': # perform compilation for i in xrange(0, 2): # two times for TOC (sorry) dir, file = os.path.split(self.path) cmd = 'pdflatex -halt-on-error %s' % file if dir == "": dir = "." process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=dir) out, err = process.communicate('') if process.returncode <> 0: sys.stdout.write(out) sys.stderr.write(err) return # display result file, ext = os.path.splitext(self.path) if ext == ".tex": path = file else: path = self.path path = path + ".pdf" print "SUCCESS: result in %s" % path else: common.onError('unknown output: %s' % output)
self.addFile(fpath) self.to_files[fpath] = "" return fpath def copy_friend(self, spath, tpath): """Load a friend file in the generation location. path -- relative path to write to. base -- absolute file to the file to copy.""" tpath = self.prepare_friend(tpath) try: shutil.copyfile(spath, tpath) return tpath except shutil.Error, e: common.onError('can not copy "%s" to "%s": %s' % (spath, tpath, str(e))) except IOError, e: common.onError('can not copy "%s" to "%s": %s' % (spath, tpath, str(e))) def use_friend(self, path, base = ''): """Ensure that a friend is available. If not, get it from the given path. If absolute, the file is loaded in the import directory and a relative path (to the document directory) is returned. path -- path to the file base -- base directory containing the file ('' for CWD files)""" # already declared? tpath = self.get_friend(path, base) if tpath: return tpath