def produce_file(sourcefile,CONFIG): # Read in config C = yaml.load(open(CONFIG)) RST_WRITER_NAME = C['rst-writer'] RST_OPTIONS = C['rst-options'] TEX_WRITE = C['tex-writer'].split(' ') RENAME_RULE = C['rename-rule'] SOURCE=os.path.expanduser(sourcefile) # Compute path and base name SRC_PATH = os.path.dirname(SOURCE) SRC_BASE = os.path.basename(SOURCE).replace('.rst','') TEX = os.path.join(SRC_PATH,SRC_BASE+'.tex') # Produce the LaTeX file publish_file(source_path=SOURCE, destination_path=TEX, writer_name=RST_WRITER_NAME, settings_overrides=RST_OPTIONS) # Produce the PDF file if subprocess.call(TEX_WRITE+[TEX])==0: # Rename it per the rewrite rule rewritten = rewrite(SRC_BASE,RENAME_RULE) os.rename(os.path.join(SRC_PATH,'out',SRC_BASE+'.pdf'), os.path.join(SRC_PATH,rewritten)) # Move tex to out shutil.move(TEX, os.path.join(SRC_PATH,'out',SRC_BASE+'.tex')) return rewritten
def generate_report(self, output_file): Logger.log_verbose( "Writing OpenOffice report to file: %s" % output_file) # Load docutils. with catch_warnings(record=True): from docutils.core import publish_file from docutils.writers.odf_odt import Writer, Reader # Create a temporary file for the reStructured Text report. with tempfile(suffix=".rst") as filename: # Generate the report in reStructured Text format. Logger.log_more_verbose("Writing temporary file in rST format...") with open(filename, "w") as source: self.write_report_to_open_file(source) # Convert to OpenOffice format. Logger.log_more_verbose("Converting to OpenOffice format...") with open(filename, "rU") as source: writer = Writer() reader = Reader() with catch_warnings(record=True): with open(output_file, "wb") as destination: publish_file( source = source, destination = destination, destination_path = output_file, reader = reader, writer = writer, )
def render_changelog_as_md( target_filename, config_filename, version, sections_only ): Environment.register(DefaultEnvironment) setup_docutils() if sections_only: def receive_sections(version_string, text): print(text) else: receive_sections = None writer = Writer(limit_version=version, receive_sections=receive_sections) settings_overrides = { "changelog_env": DefaultEnvironment(config_filename), "report_level": 3, } with open(target_filename, encoding="utf-8") as handle: if receive_sections: publish_string( handle.read(), source_path=target_filename, writer=writer, settings_overrides=settings_overrides, ) else: publish_file( handle, writer=writer, settings_overrides=settings_overrides )
def process_rst(self, directory, name): if name.startswith('pep-'): publisher = 'PEPs' else: publisher = '.rst' settings = self.get_settings(publisher, directory) pub_struct = self.publishers[publisher] if settings.prune and (directory in settings.prune): return 1 settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4]+'.html' if settings.outpath: # FIXME: we should probably try and recreate the exising structure here, # but that's more work than we need right now. settings._destination = os.path.join(settings.outpath, os.path.basename(settings._destination)) if not self.initial_settings.silent: print >>sys.stderr, ' ::: Processing:', name sys.stderr.flush() try: core.publish_file(source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name='restructuredtext', writer_name=pub_struct.writer_name, settings=settings) except ApplicationError, error: print >>sys.stderr, (' Error (%s): %s' % (error.__class__.__name__, error))
def __generate_report(self, output_file): Logger.log_verbose( "Writing LaTeX report to file: %s" % output_file) # Load docutils. with warnings.catch_warnings(record=True): from docutils.core import publish_file # Create a temporary file for the reStructured Text report. with tempfile(suffix=".rst") as filename: # Generate the report in reStructured Text format. Logger.log_more_verbose("Writing temporary file in rST format...") with open(filename, "w") as source: self.write_report_to_open_file(source) # Convert to LaTeX format. Logger.log_more_verbose("Converting to LaTeX format...") with open(filename, "rU") as source: with warnings.catch_warnings(record=True): with open(output_file, "wb") as destination: publish_file( source = source, destination = destination, destination_path = output_file, writer_name = "latex", )
def process_txt(self, directory, name): if name.startswith('pep-'): publisher = 'PEPs' else: publisher = '.txt' settings = self.get_settings(publisher, directory) pub_struct = self.publishers[publisher] dir_name = os.path.basename(directory) if (settings.prune and (directory in settings.prune or dir_name in settings.prune)): return 1 settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4]+'.html' if not self.initial_settings.silent: print >>sys.stderr, ' ::: Processing:', name sys.stderr.flush() try: core.publish_file(source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name='restructuredtext', writer_name=pub_struct.writer_name, settings=settings) except ApplicationError, error: print >>sys.stderr, (' Error (%s): %s' % (error.__class__.__name__, error))
def process_txt(self, directory, name): if name.startswith('pep-'): publisher = 'PEPs' else: publisher = '.txt' settings = self.get_settings(publisher, directory) pub_struct = self.publishers[publisher] if settings.prune and (directory in settings.prune): return 1 settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4] + '.html' if not self.initial_settings.silent: print >> sys.stderr, ' ::: Processing:', name sys.stderr.flush() try: if not settings.dry_run: core.publish_file(source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name='restructuredtext', writer_name=pub_struct.writer_name, settings=settings) except ApplicationError, error: print >> sys.stderr, (' Error (%s): %s' % (error.__class__.__name__, error))
def create_html_from_rst(rst_filename, html_filename): #os.system('') with open(rst_filename, 'r') as source, open(html_filename, 'w') as destination: core.publish_file(source=source, destination=destination, writer_name='html')
def save_to_media_docutils(request, rst_template, context, format, filename=None): from docutils.core import publish_file, publish_programmatically if not filename: filename = _get_default_filename() final_filename = '%s.%s'%(filename, format) destination = os.path.join(settings.MEDIA_ROOT, 'resume_download') destination_final = os.path.join(destination, final_filename) source_rst = write_rst(request, rst_template, context, filename) # Finally, convert to the desired format. if format == 'rst': destination_final = source_rst else: logger.debug("Converting %s to %s"%(source_rst, destination_final)) publish_file( source_path=source_rst, destination_path=destination_final, #reader_name='standalone', # default writer_name=str(format) ) media_link = settings.MEDIA_URL + 'resume_download/' + final_filename logger.debug("Media link for resume is %s"%media_link) return media_link
def make_install_pod_manpages(self, directory, target=os.path.join("share", "man")): directory = os.path.abspath(directory) files = [os.path.join(directory, i) for i in os.listdir(directory)] for f in files: basef = os.path.basename(f) if os.path.isdir(f) or not re.match(".*\.\d\.rst", basef): continue (name, section, pod) = basef.split(".") fullname = "%s.%s" % (os.path.join(directory, name), section) print("Generating man page %s" % os.path.basename(fullname)) # Generate the manpage using python 3 docutils publish_file(writer=manpage.Writer(), source_path=f, destination_path=fullname) try: gzip(fullname) except: print("*** Could not gzip %s, skipping" % os.path.basename(fullname)) continue print("Installing man page %s" % (os.path.basename(fullname) + ".gz")) if self.prefix.startswith("/"): prefix = self.prefix[1:] else: prefix = self.prefix install_path = os.path.join(self.root, prefix, target, "man" + section) mkpath(install_path) move_file(fullname + ".gz", install_path)
def publish_html_file(rst_file_path, html_out_path, css_path=None): """ Format reStructuredText in `rst_file_path` to html using `docutils` if available. Otherwise, does nothing. Parameters ---------- rst_file_path: string html_out_path: string css_path: string or None (default) If not None, use the CSS stylesheet. Returns ------- None """ try: from docutils.core import publish_file except Exception: return settings = _get_settings(css_path) publish_file(source_path=rst_file_path, destination_path=html_out_path, writer_name='html', settings_overrides=settings)
def run(self): # Go through each "directory" entry. new_data_files = [] for data in self.data_files: # Skip over files not destined for man1 directories. if not isinstance(data, tuple) or data[0] != 'share/man/man1': new_data.append(data) continue # Go through the files, and look for *.rst pages. new_list = [] for entry in data[1]: # Only process *.rst files. if not entry.endswith('.rst'): new_list.append(entry) continue # Use docutils to convert the RST document to manpage 1 format. man = entry[:-4] + '.1' log.info("converting %s -> %s" % (entry, man)) publish_file(source_path=entry, destination_path=man, writer=manpage.Writer()) new_list.append(man) new_data_files.append((data[0], new_list)) # Pass the processed list to the original install_data.run() self.data_files = new_data_files install_data.run(self)
def rst2pdf(filename, bibtex='references.bib'): """Creates a PDF from a file written in restructuredText by using""" filename = Path(filename) # Ensure that the requested file exists if not filename.exists(): print("Error: File %s does not exist" % filename, file=sys.stderr) return 1 # Create build directory if not builddir.exists(): builddir.mkdir(mode=0o755) destination = builddir / "{filename.stem}.tex".format(filename=filename) # Compile restructuredText to LaTex publish_file(source_path=str(filename), destination_path=str(destination), writer=Writer(), settings_overrides={'template': 'template.tex'}) # Copy BibTex references bibtex = Path(bibtex) if bibtex.exists(): copy(str(bibtex), str(builddir / bibtex.name)) # Compile LaTex to PDF # FIXME As long as latexmk is not installable on the working machine, we use # three xelatex calls for building outlines and bibtex # check_call(['latexmk', '-xelatex', destination.name], cwd=str(builddir)) check_call(['xelatex', destination.name], cwd=str(builddir)) check_call(['xelatex', destination.name], cwd=str(builddir)) check_call(['bibtex', destination.stem + '.aux'], cwd=str(builddir)) check_call(['xelatex', destination.name], cwd=str(builddir))
def _build_simple_rst(self, package_name, tag_name): """ Build HTML output from the setuptools long_description """ rst = '' package_path = os.path.join(self.options.workingdir, package_name) tag_folder = os.path.join(package_path, tag_name) if os.path.isdir(tag_folder) and 'setup.py' in os.listdir(tag_folder): cmd = 'PYTHONPATH="%s" %s %s/setup.py --long-description' % ( ':'.join(sys.path), sys.executable, tag_folder) rst = shell_cmd(cmd, fromwhere=tag_folder) if rst and rst != 'UNKNOWN': build_folder = os.path.join(tag_folder, '.docbuilder_html') shutil.rmtree(build_folder, ignore_errors=True) os.mkdir(build_folder) output_path = os.path.join(build_folder, 'index.html') settings = {} if os.path.isfile(self.options.fallback_css): settings = {'stylesheet_path': self.options.fallback_css} try: publish_file(source=StringIO(rst), writer_name='html', destination_path=output_path, settings_overrides=settings) self.packages[package_name][tag_name] = build_folder msg = 'Building simple ReST docs for %s %s.' LOG.info(msg % (package_name, tag_name)) except SystemMessage as e: msg = 'Building simple ReST doc for %s %s failed!' LOG.error(msg % (package_name, tag_name)) LOG.error(str(e)) pass
def generate_report(self, output_file): Logger.log_verbose("Writing OpenOffice report to file: %s" % output_file) # Load docutils. with catch_warnings(record=True): from docutils.core import publish_file from docutils.writers.odf_odt import Writer, Reader # Create a temporary file for the reStructured Text report. with tempfile(suffix=".rst") as filename: # Generate the report in reStructured Text format. Logger.log_more_verbose("Writing temporary file in rST format...") with open(filename, "w") as source: self.write_report_to_open_file(source) # Convert to OpenOffice format. Logger.log_more_verbose("Converting to OpenOffice format...") with open(filename, "rU") as source: writer = Writer() reader = Reader() with catch_warnings(record=True): with open(output_file, "wb") as destination: publish_file( source=source, destination=destination, destination_path=output_file, reader=reader, writer=writer, )
def gen_one_addon_index(readme_filename): addon_dir = os.path.dirname(readme_filename) index_dir = os.path.join(addon_dir, 'static', 'description') index_filename = os.path.join(index_dir, 'index.html') if os.path.exists(index_filename): with open(index_filename) as f: if 'oca-gen-addon-readme' not in f.read(): # index was created manually return if not os.path.isdir(index_dir): os.makedirs(index_dir) publish_file( source_path=readme_filename, destination_path=index_filename, writer_name='html4css1', settings_overrides=RST2HTML_SETTINGS, ) with open(index_filename, "rb") as f: index = f.read() # remove the docutils version from generated html, to avoid # useless changes in the readme index = re.sub( rb'(<meta.*generator.*Docutils)\s*[\d.]+', rb"\1", index, re.MULTILINE ) with open(index_filename, "wb") as f: f.write(index) return index_filename
def process_txt(self, directory, name): if name.startswith("pep-"): publisher = "PEPs" else: publisher = ".txt" settings = self.get_settings(publisher, directory) pub_struct = self.publishers[publisher] if settings.prune and directory in settings.prune: return True settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4] + ".html" if not self.initial_settings.silent: print >>sys.stderr, " ::: Processing:", name sys.stderr.flush() try: core.publish_file( source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name="restructuredtext", writer_name=pub_struct.writer_name, settings=settings, ) except ApplicationError, error: print >>sys.stderr, (" Error (%s): %s" % (error.__class__.__name__, error))
def process_txt(self, directory, name): if name.startswith("pep-"): publisher = "PEPs" else: publisher = ".txt" settings = self.get_settings(publisher, directory) errout = ErrorOutput(encoding=settings.error_encoding) pub_struct = self.publishers[publisher] settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4] + ".html" if not self.initial_settings.silent: errout.write(" ::: Processing: %s\n" % name) sys.stderr.flush() try: if not settings.dry_run: core.publish_file( source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name="restructuredtext", writer_name=pub_struct.writer_name, settings=settings, ) except ApplicationError: error = sys.exc_info()[1] # get exception in Python <2.6 and 3.x errout.write(" %s\n" % ErrorString(error))
def run(self): import sphinx from sphinx import apidoc sphinx_args = ['-P', '-n', '-c', 'documentation/', '.'] apidoc_args = [] if self.cathartic: sphinx_args = ['-a', '-E'] + sphinx_args apidoc_args = ['--force'] + apidoc_args apidoc.main( ['sphinx-apidoc', '--output-dir=fipy/generated', '--suffix=rst'] + apidoc_args + ['fipy']) apidoc.main([ 'sphinx-apidoc', '--output-dir=documentation/tutorial/package/generated', '--suffix=rst' ] + apidoc_args + ['documentation/tutorial/package']) if self.html: sphinx.main(['sphinx-build', '-b', 'redirecting_html'] + sphinx_args + ['documentation/_build/html/']) if self.pdf: try: sphinx.main(['sphinx-build', '-b', 'latex'] + sphinx_args + ['documentation/_build/latex/']) except SystemExit: pass outdir = os.path.join('documentation', '_build', 'latex') from docutils.core import publish_file for xtra in ("LICENSE", "DISCLAIMER"): publish_file(source_path="%s.rst" % xtra, destination_path=os.path.join( outdir, "%s.tex" % xtra), reader_name='standalone', parser_name='restructuredtext', writer_name='latex', settings_overrides={ 'template': 'documentation/_templates/empty.tex' }) savedir = os.getcwd() os.chdir(outdir) os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("makeindex -s python.ist fipy") os.system("makeindex -s python.ist modfipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.chdir(savedir)
def process_txt(self, directory, name): if name.startswith('pep-'): publisher = 'PEPs' else: publisher = '.txt' settings = self.get_settings(publisher, directory) errout = ErrorOutput(encoding=settings.error_encoding) pub_struct = self.publishers[publisher] if settings.prune and (directory in settings.prune): return 1 settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4]+'.html' if not self.initial_settings.silent: print >>errout, ' ::: Processing: %s' % name sys.stderr.flush() try: if not settings.dry_run: core.publish_file(source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name='restructuredtext', writer_name=pub_struct.writer_name, settings=settings) except ApplicationError, error: print >>errout, ' %s' % ErrorString(error)
def _generate_html(self, rst_file_path, result_html_path): """ Generates a HTML file based on the input RST file. """ if not os.path.exists(rst_file_path): raise DocGenerateException( "Provided RST file: %s does not exists." % rst_file_path) try: style_files = str( os.path.join(self._dist_styles_folder, self.ORIGINAL_CSS_STYLE) + "," + os.path.join(self._dist_styles_folder, self.HTML_CSS_STYLE)) overrides = { 'stylesheet_path': style_files, 'date': False, 'time': False, 'math_output': 'MathJax', 'embed_stylesheet': False } publish_file(source_path=rst_file_path, destination_path=result_html_path, writer_name="html", settings_overrides=overrides) self.logger.debug("HTML file %s generated successfully." % result_html_path) except Exception, ex: self.logger.exception("Could not generate HTML documentation") raise DocGenerateException("Could not generate HTML documentation", ex)
def check_rst(readme_filename): with tempfile.NamedTemporaryFile() as f: publish_file( source_path=readme_filename, destination=f, writer_name='html4css1', settings_overrides=RST2HTML_SETTINGS, )
def create_html_from_rst(rst_filename, html_filename): #os.system('') from docutils import core source = open(rst_filename, 'r') destination = open(html_filename, 'w') core.publish_file(source=source, destination=destination, writer_name='html') source.close() destination.close()
def publish(rst, rst_file, html_file): with rst_file.open('w') as f: f.write('\n'.join(rst)) publish_file(source_path=str(rst_file), destination_path=str(html_file), writer_name='html')
def rst2html(i, o): with open(i, "r") as in_file: with open(o, "w") as out_file: publish_file(source=in_file, destination=out_file, reader_name="standalone", parser_name="restructuredtext", writer_name="html", settings_overrides=stylesheets)
def rst_check(): """Check syntax of reST-formatted files.""" for file in glob('*.rst'): try: publish_file(open(file), destination=StringIO(), settings_overrides={'halt_level': 1}) except SystemMessage: exit(1) print(success('All reST files pass!'))
def deal(source_dir): print 'source dir %s' % source_dir for f in glob.glob('%s/*.txt' % source_dir): fi=open(f) filename, ext = os.path.splitext(f) filename = filename+'.htm' print '\tprocessing %s to %s' % (f, filename) fo=open(filename, "w") # publish_file(source=fi, destination=fo, writer_name='html', settings_overrides={'output_encoding': 'cp936'}) publish_file(source=fi, destination=fo, writer_name='html')
def process(self): def skip_setting(key): in_base_filter = key in DexyFilter._settings in_skip = key in self.setting( self.skip_settings) or key == self.skip_settings return in_base_filter or in_skip settings_overrides = dict( (k.replace("-", "_"), v) for k, v in self.setting_values().iteritems() if v and not skip_setting(k)) writer_name = self.docutils_writer_name() warning_stream = StringIO.StringIO() settings_overrides['warning_stream'] = warning_stream self.log_debug("settings for rst: %r" % settings_overrides) self.log_debug("rst writer: %s" % writer_name) # Check that template extension matches output. if 'template' in settings_overrides and not self.setting( 'allow-any-template-extension'): template = settings_overrides['template'] template_ext = os.path.splitext(template)[1] if not template_ext == self.ext: msg = "You requested template '%s' with extension '%s' for %s, does not match document extension of '%s'" args = (template, template_ext, self.key, self.ext) raise dexy.exceptions.UserFeedback(msg % args) if not 'template' in settings_overrides: if hasattr(writer_name, 'default_template'): settings_overrides['template'] = default_template(writer_name) try: core.publish_file( source_path=self.input_data.storage.data_file(), destination_path=self.output_data.storage.data_file(), writer_name=writer_name, settings_overrides=settings_overrides) except ValueError as e: if "Invalid placeholder in string" in e.message and 'template' in settings_overrides: self.log_warn("you are using template '%s'. is this correct?" % settings_overrides['template']) raise except Exception as e: self.log_warn( "An error occurred while generating reStructuredText.") self.log_warn("source file %s" % (self.input_data.storage.data_file())) self.log_warn("settings for rst: %r" % settings_overrides) self.log_warn("rst writer: %s" % writer_name) raise self.log_debug("docutils warnings:\n%s\n" % warning_stream.getvalue())
def goProcess(self, event): # gather the data inp = self.cp.teSource.GetValue() out = self.cp.teDestin.GetValue() format = self.cp.rbFormat.GetStringSelection() # process and show wn = FORMATS[format][0] publish_file(source_path=inp, destination_path=out, writer_name=wn) self.tp.setDestinText(out) return
def rst2html(i, o): with open(i, "r") as in_file: with open(o, "w") as out_file: publish_file( source=in_file, destination=out_file, reader_name="standalone", parser_name="restructuredtext", writer_name="html", settings_overrides=stylesheets )
def deal(source_dir): print 'source dir %s' % source_dir for f in glob.glob('%s/*.txt' % source_dir): fi = open(f) filename, ext = os.path.splitext(f) filename = filename + '.htm' print '\tprocessing %s to %s' % (f, filename) fo = open(filename, "w") # publish_file(source=fi, destination=fo, writer_name='html', settings_overrides={'output_encoding': 'cp936'}) publish_file(source=fi, destination=fo, writer_name='html')
def test_image_directive_run_default_base_uri(ditaa_wrapper_class_mock, integration_output_path): # pylint: disable=redefined-outer-name,unused-argument ditaa.register(integration_output_path, '.') output = integration_output_path.joinpath('sample.html') publish_file(source_path=str(REST_SAMPLE_FILE), destination_path=str(output), writer_name='html5') with output.open(encoding='utf8') as data: soup = BeautifulSoup(data, 'html.parser') images = soup.find_all('img') assert images[0]['src'] == './fake_hash_code.png'
def run (self): import sphinx from sphinx import apidoc sphinx_args = ['-P', '-n', '-c', 'documentation/', '.'] apidoc_args = [] if self.cathartic: sphinx_args = ['-a', '-E'] + sphinx_args apidoc_args = ['--force'] + apidoc_args apidoc.main(['sphinx-apidoc', '--output-dir=fipy/generated', '--suffix=rst'] + apidoc_args + ['fipy']) apidoc.main(['sphinx-apidoc', '--output-dir=documentation/tutorial/package/generated', '--suffix=rst'] + apidoc_args + ['documentation/tutorial/package']) if self.html: sphinx.main(['sphinx-build', '-b', 'redirecting_html'] + sphinx_args + ['documentation/_build/html/']) if self.pdf: try: sphinx.main(['sphinx-build', '-b', 'latex'] + sphinx_args + ['documentation/_build/latex/']) except SystemExit: pass outdir = os.path.join('documentation', '_build', 'latex') from docutils.core import publish_file for xtra in ("LICENSE", "DISCLAIMER"): publish_file(source_path="%s.rst" % xtra, destination_path=os.path.join(outdir, "%s.tex" % xtra), reader_name='standalone', parser_name='restructuredtext', writer_name='latex', settings_overrides= { 'template': 'documentation/_templates/empty.tex' }) savedir = os.getcwd() os.chdir(outdir) os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("makeindex -s python.ist fipy") os.system("makeindex -s python.ist modfipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.chdir(savedir)
def generate_tex(infile, outfile, template, title, beamer): publish_file(source_path=infile, destination_path=outfile, writer_name='latex', settings_overrides={'template': template, 'anchor': False}) replace('\\\\phantomsection%\\n \\n', '', outfile) replace('\\n\\n}', '}', outfile) replace('includegraphics{', 'includegraphics\[width=\\\\linewidth\]{', outfile) replace('THETITLE', title, outfile) if beamer: replace('%\\n \\\\label{[a-z0-9-]*}%\\n}\\n%*', '}', outfile) replace('section{', 'end\{frame\}\\n\\\\begin\{frame\}\{', outfile)
def run(self): import sphinx from sphinx import apidoc sphinx_args = ["-P", "-n", "-c", "documentation/", "."] apidoc_args = [] if self.cathartic: sphinx_args = ["-a", "-E"] + sphinx_args apidoc_args = ["--force"] + apidoc_args apidoc.main(["sphinx-apidoc", "--output-dir=fipy/generated", "--suffix=txt"] + apidoc_args + ["fipy"]) apidoc.main( ["sphinx-apidoc", "--output-dir=documentation/tutorial/package/generated", "--suffix=txt"] + apidoc_args + ["documentation/tutorial/package"] ) if self.html: sphinx.main(["sphinx-build", "-b", "redirecting_html"] + sphinx_args + ["documentation/_build/html/"]) if self.pdf: sphinx.main(["sphinx-build", "-b", "latex"] + sphinx_args + ["documentation/_build/latex/"]) outdir = os.path.join("documentation", "_build", "latex") from docutils.core import publish_file for xtra in ("LICENSE", "DISCLAIMER"): publish_file( source_path="%s.txt" % xtra, destination_path=os.path.join(outdir, "%s.tex" % xtra), reader_name="standalone", parser_name="restructuredtext", writer_name="latex", settings_overrides={"template": "documentation/_templates/empty.tex"}, ) savedir = os.getcwd() os.chdir(outdir) os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.system("makeindex -s python.ist fipy") os.system("makeindex -s python.ist modfipy") os.system("pdflatex fipy") os.system("pdflatex fipy") os.chdir(savedir)
def run(win): from docutils.core import publish_file import os.path import StringIO filename = win.document.filename fi=StringIO.StringIO(win.document.GetText().encode(win.document.locale)) f, ext = os.path.splitext(filename) htmlfile = f+'.htm' fo=open(htmlfile, "w") publish_file(source=fi, destination=fo, writer_name='html') fi.close() fo.close() win.SetStatusText('Success!')
def rst2html(i, o, stylesheets): log("rst2html %s -> %s" % (i, o)) with open(i, "r") as in_file: with open(o, "w") as out_file: publish_file( source=in_file, destination=out_file, reader_name="standalone", parser_name="restructuredtext", writer_name="html", settings_overrides={ "stylesheet_path": stylesheets, }, )
def rst2html(i, o, stylesheets): log("rst2html %s -> %s" % (i, o)) with open(i, "r", encoding="utf-8") as in_file: with open(o, "w", encoding="utf-8") as out_file: publish_file( source=in_file, destination=out_file, reader_name="standalone", parser_name="restructuredtext", writer_name="html", settings_overrides={ "stylesheet_path": stylesheets, }, )
def run(win): from docutils.core import publish_file import os.path import StringIO filename = win.document.filename fi = StringIO.StringIO(win.document.GetText().encode(win.document.locale)) f, ext = os.path.splitext(filename) htmlfile = f + '.htm' fo = open(htmlfile, "w") publish_file(source=fi, destination=fo, writer_name='html') fi.close() fo.close() win.SetStatusText('Success!')
def rebuild(rst,php): read_txt = open(rst,'r') read_html = open(php,'w') html = Writer() publish_file(writer=html, source=read_txt, destination=read_html) read_html = open(php,'r') data = read_html.readlines() read_html.close() data.insert(0,"<script language=\"php\">$title=\"Comics Grabber\";$TOPDIR=\"../../\";include $TOPDIR.\"top.php3\";</script>") data.append("<script language=\"php\">include $TOPDIR.\"bottom.php3\";</script>") read_html = open(php,'w') read_html.writelines(data) read_html.close()
def run(self): man_dir = os.path.join(os.path.dirname(os.curdir), 'man') for path, names, filenames in os.walk(man_dir): for f in filenames: if f.endswith('.rst'): filename, section, ext = f.rsplit('.', 2) dst_dir = os.path.join('build', 'man', 'man' + section) if not os.path.exists(dst_dir): os.makedirs(dst_dir) src = os.path.join(path, f) dst = os.path.join(dst_dir, filename + '.' + section) print("converting {0}".format(src)) publish_file(source_path=src, destination_path=dst, writer_name='manpage')
def run(self): src_dir = os.path.join(os.path.dirname(os.curdir), "man") dst_dir = os.path.join("build", "man") for path, names, filenames in os.walk(src_dir): for f in filenames: if f.endswith(".rst"): filename, section, ext = f.rsplit(".", 2) if not os.path.exists(dst_dir): os.makedirs(dst_dir) src = os.path.join(path, f) dst = os.path.join(dst_dir, filename + "." + section) print("converting {0}".format(src)) publish_file(source_path=src, destination_path=dst, writer_name="manpage") man_dir = os.path.join("share", "man", "man" + section) self.distribution.data_files.append((man_dir, [dst]))
def render_source(source_path, *, base_path, build_path): sp = source_path.relative_to(base_path) destination_path = build_path / sp.parent / sp.with_suffix('').name / "index.html" os.makedirs(destination_path.parent, exist_ok=True) print(f"rendering {source_path=!s} to {destination_path=!s}") publish_file( writer_name='html5', source_path=source_path, destination_path=destination_path, settings_overrides=dict( stylesheet_path='./static/css/bluespan-normalize.css', template='./static/template/mobile.tmpl', ) )
def gen_html(): """Generate HTML output.""" rst_files = glob('*.rst') dep(map(lambda s: s[:-4] + '.html', rst_files), rst_files, mapping=True) for file in glob('*.rst'): html_file = file[:-4] + '.html' if newer(html_file, file): break try: publish_file(open(file), destination=open(html_file, 'w'), settings_overrides={'halt_level': 1}) except SystemMessage: exit(1) print(success('%s generated!' % file)) print(success('All reST generated!'))
def check_rst2html(path): """ Checks for warnings when doing ReST to HTML conversion """ # pylint: disable=import-error,import-outside-toplevel from contextlib import redirect_stderr # Import here because it breaks <= Python 3.4 from docutils.core import publish_file # Import here because only available in doc tests stderr = io.StringIO() # This will exit with status if there is a bad enough error with redirect_stderr(stderr): output = publish_file(source_path=path, writer_name='html', enable_exit_status=True, destination_path='/dev/null') warning_text = stderr.getvalue() if warning_text or not output: print(warning_text) return 1 return 0
def rst2latex(fn_rst): """Create latex file with docutils""" fn_tex = os.path.splitext(fn_rst)[0] + '.tex' with open(fn_rst) as foi: with open(fn_tex, 'w') as foo: t = publish_file(source = foi, destination=foo, writer_name='latex') return fn_tex
def generate_report(self, output_file): Logger.log_verbose("Writing OpenOffice report to file: %s" % output_file) # Generate the report in reStructured Text format. source = StringIO() self.write_report_to_open_file(source) source.seek(0) # Convert to OpenOffice format. writer = Writer() reader = Reader() with warnings.catch_warnings(record=True): with open(output_file, "wb") as destination: publish_file( source=source, destination=destination, destination_path=output_file, reader=reader, writer=writer )
def exportPDF(self, workingFile, explicit=False): """Export given reST-file as pdf. Name of pdf-file is the same as name of given txt-file. Requires an installed LaTeX-environment. Return False if pdfLaTeX isn't installed, else return True. Keyword arguments: workingFile -- path and name of reST-file (string) explicit -- indicates, if pdf-export is the only (explicit) export. When true, the html-, xml-, tex- and dvi-files with the same name will be deleted, otherwise they'll remain. """ outputFile = open(workingFile[:-3]+"tex", "w") outputFile.write(publish_file(open(workingFile, "r"), writer_name="latex")) outputFile.close() try: os.chdir(os.path.dirname(workingFile)) # change working-directory into that one, that contains the tex-file if not win32process: subprocess.Popen(('latex', workingFile[:-3]+"tex")).wait() else: subprocess.Popen(('pdflatex', workingFile[:-3]+"tex"), creationflags=win32process.CREATE_NO_WINDOW).wait() except WindowsError: return False fileEndings = ["out", "log", "aux"] for fileEnding in fileEndings: try: os.remove(workingFile[:-3]+fileEnding) except OSError: pass if explicit: self.cleanFilesUp(workingFile, True, None, "pdf") return True
def exportDVI(self, workingFile, explicit=False): """Export given reST-file as dvi. Name of dvi-file is the same as name of given txt-file. Requires an installed LaTeX-environment. Return False if LaTeX isn't installed, else return True. Keyword arguments: workingFile -- path and name of reST-file (string) explicit -- indicates, if dvi-export is the only (explicit) export. When true, the html-, xml-, tex- and pdf-files with the same name will be deleted, otherwise they'll remain. """ outputFile = open(workingFile[:-3] + "tex", "w") outputFile.write( publish_file(open(workingFile, "r"), writer_name="latex")) outputFile.close() try: os.chdir( os.path.dirname(workingFile) ) # change working-directory into that one, that contains the tex-file if not win32process: subprocess.Popen(('latex', workingFile[:-3] + "tex")).wait() else: subprocess.Popen( ('latex', workingFile[:-3] + "tex"), creationflags=win32process.CREATE_NO_WINDOW).wait() except WindowsError: return False if explicit: self.cleanFilesUp(workingFile, True, None, "dvi") return True
def publish_xml_cmdline (in_path = None, in_string = None, out_path = None, settings_overrides=None ): try: import locale locale.setlocale(locale.LC_ALL, '') except: pass from docutils.core import default_description, default_usage, publish_file, publish_string import docutils.core description = ('Generates Docutils-native XML from standalone ' 'reStructuredText sources. ' + default_description) if not in_path and not in_string: raise TypeError('publish_xml_cmdlind() must have either "in_path" or "in_string" as parameters') if in_path: out_string = publish_file(source_path= in_path, destination_path=out_path, settings_overrides = settings_overrides, writer_name='xml') elif in_string: out_string = publish_string(source= in_string, destination_path=out_path, settings_overrides = settings_overrides, writer_name='xml') return out_string
def rst2something(target, source, env, validOptions, writer): if not has_docutils: return # Find variables that can be passed to docutils. settings = dict([item for item in env.items() if item[0] in validOptions]) # stylesheet and stylesheet_path are mutually exclusive if 'stylesheet' in settings: settings['stylesheet_path'] = None for i in range(len(source)): publish_file(source_path=source[i].path, destination_path=target[i].path, writer_name=writer, settings_overrides=settings)
def run(self): """build end-user documentation.""" # if options.build_dir: # docs.builddir = options.build_dir if not path.exists(docs.builddir): os.mkdir(docs.builddir) docs.state_is_api = False docsdir = path.join(docs.builddir, 'docs') if not path.exists(docsdir): os.mkdir(docsdir) teardown_examples() # clear tmp files stylesheet = path.join(docs.srcdir, 'farmdev-docutils.css') body = publish_file( open(path.join(docs.srcdir, 'index.rst'), 'r'), destination=open(path.join(docsdir, 'index.html'), 'w'), writer_name='html', settings_overrides={'stylesheet_path': stylesheet}, # settings_overrides={'halt_level':2, # 'report_level':5} ) f = open(path.join(docsdir, 'index.html'), 'w') f.write(body) f.close() shutil.copy(path.join(docs.srcdir, 'html4css1.css'), path.join(docsdir, 'html4css1.css')) shutil.copy(stylesheet, path.join(docsdir, 'farmdev-docutils.css')) images_target = path.join(docsdir, 'images') if path.exists(images_target): shutil.rmtree(images_target) shutil.copytree(path.join(docs.srcdir, 'images'), images_target) print "built user docs to %s" % docsdir
def transformRST(self, workingFile): """ Transform the content of given reST-file into html and write html-code to a file. Return filename of html-file. Return nothing if no filename is given. FAils when the txt-file is in a directory the user has no write access to Keyword arguments: workingFile -- name of reST-file (string) """ if workingFile: # only if filename is given - otherwise there would be an empty tempfile generated and not deleted #tmpFile must be created in a directory the user has write-access to -> using directory containing the txt-file. tmpFilename = "\\".join( workingFile.split("\\")[:-1] ) + "\\reSTtempFile" + str( time() ) + ".html" # i would love to use the python "tempfile"-module at this place outputFile = open( tmpFilename, "w" ) # but i can't, 'cause the generated files would be destroyed at the end of this method. outputFile.write( publish_file(open(workingFile, "r"), writer_name="html")) return tmpFilename else: return