def asciidoc2html(self): """ Convert AsciiDoc blog_file to Wordpress compatible HTML content. """ asciidoc = asciidocapi.AsciiDocAPI() asciidoc.options('--no-header-footer') asciidoc.options('--doctype', self.doctype) asciidoc.options('--attribute', 'blogpost') for attr in OPTIONS.attributes: asciidoc.options('--attribute', attr) for opt in OPTIONS.asciidoc_opts: print '%r' % opt opt = opt.partition(' ') if opt[2]: s = opt[2] s = s.strip() if (s.startswith('"') and s.endswith('"') or s.startswith("'") and s.endswith("'")): # Strip quotes. s = s[1:-1] asciidoc.options(opt[0], s) else: asciidoc.options(opt[0]) if OPTIONS.verbose > 1: asciidoc.options('--verbose') verbose('asciidoc: options: %r' % asciidoc.options.values) outfile = StringIO.StringIO() asciidoc.execute(self.blog_file, outfile, backend='wordpress') result = outfile.getvalue() result = unicode(result, 'utf8') self.content = StringIO.StringIO(result.encode('utf8')) for s in asciidoc.messages: infomsg('asciidoc: %s' % s)
def render_adoc(orig_source_f: Path, infile: StringIO): outfile = conf.outdir / orig_source_f.with_suffix(".html") outfile.parent.mkdir(parents=True, exist_ok=True) ad = asciidocapi.AsciiDocAPI() ad.attributes["author"] = conf.get("author", "") infile.seek(0) with outfile.open("w") as outf: ad.execute(infile, outf, backend="html5")
def generate_expected(self, backend): """ Generate and return test data output for backend. """ asciidoc = asciidocapi.AsciiDocAPI() asciidoc.options.values = self.options asciidoc.attributes = self.attributes infile = self.source outfile = io.StringIO() asciidoc.execute(infile, outfile, backend) return outfile.getvalue().splitlines()
def post_process(self): import StringIO infile = StringIO.StringIO(self.content) outfile = StringIO.StringIO() try: import asciidocapi except: raise error.general('installation error: no asciidocapi found') asciidoc_py = _make_path(self.sbpath, options.basepath, 'asciidoc', 'asciidoc.py') try: asciidoc = asciidocapi.AsciiDocAPI(asciidoc_py) except: raise error.general('application error: asciidocapi failed') asciidoc.execute(infile, outfile) out = outfile.getvalue() infile.close() outfile.close() return out
def main(conf, asciiDocOptions): if conf.doCaching: # Do we have a cache directory? If not, we can live without it. try: os.mkdir(conf.cacheDir) except OSError as e: if e.errno != errno.EEXIST: conf.doCaching = False if not os.access(conf.cacheDir, os.R_OK | os.W_OK): conf.doCaching = False # The data to be transformed into a readable shape for the nested AsciiDoc. data = conf.infile.read() # The name of the file that the nested AsciiDoc will create. outFile = None if conf.doCaching: h = hashlib.sha1() h.update(data) h.update("".join(conf.attributes)) h.update("".join(asciiDocOptions)) h.update(conf.backend) h.update(str(conf.numbered)) h.update(conf.startTag) h.update(conf.endTag) h.update(VERSION) outFile = os.path.join(conf.cacheDir, h.hexdigest()) else: outFile = "adextract_asciidoc_output.tmp" if not conf.doCaching or not os.path.exists(outFile): with tempfile.TemporaryFile() as tmpFile: # Fill the temporary file with the data to be processed. parseBlocks(conf, data, tmpFile) # Rewind The temporary file as the preceding call wrote into it. tmpFile.seek(0) # Create and configure asciidoc. asciidoc = asciidocapi.AsciiDocAPI() asciidoc.attributes =\ {x[0] : x[2] for x in (p.partition("=") for p in conf.attributes)} for o in asciiDocOptions: asciidoc.options(o) # Launch AsciiDoc. asciidoc.execute(tmpFile, outfile=outFile, backend=conf.backend) # Handle the result to AsciiDoc. with open(outFile, 'r') as f: conf.outfile.write(f.read()) # If there is no caching, the file from the nested AsciiDoc was not moved. if not conf.doCaching: os.remove(outFile) # Cleanup the cache in a LRU way if needed. if conf.doCaching: files = sorted([ os.path.join(conf.cacheDir, f) for f in os.listdir(conf.cacheDir) ], key=os.path.getatime) sizes = [os.path.getsize(f) for f in files] while sum(sizes) > conf.cacheSize and files: os.remove(files.pop(0)) del sizes[0]