def test_pygments_style_to_stylesheet(): def matching_style(style_sheet, token_type): match, = style_sheet.find_matches(Token('text', token_type), None) return style_sheet[match.style_name] class SimpleStyle(Style): background_color = "#f0f0f0" default_style = "" styles = { Comment: "italic #60a0b0", Keyword: "bold #007020", Number: "#40a070", } style_sheet = pygments_style_to_stylesheet(SimpleStyle) comment_style = matching_style(style_sheet, Comment) assert comment_style.keys() == set(['font_slant', 'font_color']) assert comment_style.font_slant == FontSlant.ITALIC assert comment_style.font_color == HexColor('#60a0b0') keyword_style = matching_style(style_sheet, Keyword) assert keyword_style.keys() == set(['font_weight', 'font_color']) assert keyword_style.font_weight == FontWeight.BOLD assert keyword_style.font_color == HexColor('#007020') number_style = matching_style(style_sheet, Number) assert number_style.keys() == set(['font_color']) assert number_style.font_color == HexColor('#40a070') for token in (Text, Name, Punctuation, Operator, Literal, Name.Builtin, Name.Builtin.Pseudo, Literal.String): style = matching_style(style_sheet, token) assert not style.keys()
def template_from_config(config, confdir, warn): template_cfg = {} if isinstance(config.rinoh_template, str): tmpl_path = path.join(confdir, config.rinoh_template) if path.isfile(tmpl_path): template_cfg['base'] = TemplateConfigurationFile(tmpl_path) template_cls = template_cfg['base'].template else: template_cls = DocumentTemplate.from_string(config.rinoh_template) elif isinstance(config.rinoh_template, TemplateConfiguration): template_cfg['base'] = config.rinoh_template template_cls = config.rinoh_template.template else: template_cls = config.rinoh_template if isinstance(config.rinoh_stylesheet, str): stylesheet_path = path.join(confdir, config.rinoh_stylesheet) stylesheet = StyleSheet.from_string(stylesheet_path if path.isfile( stylesheet_path) else config.rinoh_stylesheet) else: stylesheet = config.rinoh_stylesheet if config.pygments_style is not None: if stylesheet is not None: base = stylesheet elif 'base' in template_cfg: base = template_cfg['base']['stylesheet'] else: base = template_cls.stylesheet.default_value stylesheet = pygments_style_to_stylesheet(config.pygments_style, base) if stylesheet is not None: template_cfg['stylesheet'] = stylesheet language = config.language if language: try: template_cfg['language'] = Language.from_string(language) except KeyError: warn('The language "{}" is not supported by rinohtype.'.format( language)) variables = {} if config.rinoh_paper_size: variables['paper_size'] = config.rinoh_paper_size sphinx_config = template_cls.Configuration('Sphinx conf.py options', **template_cfg) sphinx_config.variables.update(variables) return sphinx_config
def template_from_config(config, confdir, warn): template_cfg = {} if isinstance(config.rinoh_template, str): tmpl_path = path.join(confdir, config.rinoh_template) if path.isfile(tmpl_path): template_cfg['base'] = TemplateConfigurationFile(tmpl_path) template_cls = template_cfg['base'].template else: template_cls = DocumentTemplate.from_string(config.rinoh_template) elif isinstance(config.rinoh_template, TemplateConfiguration): template_cfg['base'] = config.rinoh_template template_cls = config.rinoh_template.template else: template_cls = config.rinoh_template if isinstance(config.rinoh_stylesheet, str): stylesheet_path = path.join(confdir, config.rinoh_stylesheet) stylesheet = StyleSheet.from_string(stylesheet_path if path.isfile(stylesheet_path) else config.rinoh_stylesheet) else: stylesheet = config.rinoh_stylesheet if config.pygments_style is not None: if stylesheet is not None: base = stylesheet elif 'base' in template_cfg: base = template_cfg['base']['stylesheet'] else: base = template_cls.stylesheet.default_value stylesheet = pygments_style_to_stylesheet(config.pygments_style, base) if stylesheet is not None: template_cfg['stylesheet'] = stylesheet language = config.language if language: try: template_cfg['language'] = Language.from_string(language) except KeyError: warn('The language "{}" is not supported by rinohtype.') variables = {} if config.rinoh_paper_size: variables['paper_size'] = config.rinoh_paper_size sphinx_config = template_cls.Configuration('Sphinx conf.py options', **template_cfg) sphinx_config.variables.update(variables) return sphinx_config
def write_doc(self, docname, doctree, docnames, targetname): config = self.config suffix, = config.source_suffix source_path = os.path.join(self.srcdir, docname + suffix) parser = ReStructuredTextReader() rinoh_tree = parser.from_doctree(source_path, doctree) rinoh_document_template = config.rinoh_document_template template = (DocumentTemplate.from_string(rinoh_document_template) if isinstance(rinoh_document_template, str) else rinoh_document_template) if isinstance(self.config.rinoh_stylesheet, str): stylesheet = StyleSheet.from_string(self.config.rinoh_stylesheet) elif self.config.rinoh_stylesheet is None: stylesheet = template.Configuration.stylesheet.default_value else: stylesheet = self.config.rinoh_stylesheet if config.pygments_style is not None: stylesheet = pygments_style_to_stylesheet(config.pygments_style, stylesheet) paper_size = config.rinoh_paper_size base_config = template.Configuration(paper_size=paper_size, stylesheet=stylesheet) if config.rinoh_template_configuration is not None: template_configuration = config.rinoh_template_configuration if template_configuration.base is None: template_configuration.base = base_config else: template_configuration = base_config rinoh_document = template(rinoh_tree, configuration=template_configuration, backend=pdf) extra_indices = StaticGroupedFlowables(self.generate_indices(docnames)) rinoh_document.insert('indices', extra_indices, 0) rinoh_logo = config.rinoh_logo if rinoh_logo: rinoh_document.metadata['logo'] = rinoh_logo rinoh_document.metadata['title'] = doctree.settings.title rinoh_document.metadata['subtitle'] = ('Release {}'.format( config.release)) rinoh_document.metadata['author'] = doctree.settings.author outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)