def apply(self, **kwargs: Any) -> None:
     substitution_defs = self.document.substitution_defs
     version = substitution_defs['version'].astext()
     pad = " " * len(version)
     substitution_defs['version-spacepad'] = nodes.substitution_definition(
         version, pad)
     ...
    def add_document_wordcount(self) -> None:
        """Add the wordcount, generated by the ``mdit_py_plugins.wordcount_plugin``."""

        wordcount_metadata = self.md_env.get("wordcount", {})
        if not wordcount_metadata:
            return

        # save the wordcount to the sphinx BuildEnvironment metadata
        if self.sphinx_env is not None:
            meta = self.sphinx_env.metadata.setdefault(self.sphinx_env.docname,
                                                       {})
            meta["wordcount"] = wordcount_metadata

        # now add the wordcount as substitution definitions,
        # so we can reference them in the document
        for key in ("words", "minutes"):
            value = wordcount_metadata.get(key, None)
            if value is None:
                continue
            substitution_node = nodes.substitution_definition(
                str(value), nodes.Text(str(value)))
            substitution_node.source = self.document["source"]
            substitution_node["names"].append(f"wordcount-{key}")
            self.document.note_substitution_def(substitution_node,
                                                f"wordcount-{key}")
Esempio n. 3
0
    def apply(self):
        settings = self.document.settings
        if not hasattr(settings, 'cc_embed') or not settings.cc_embed:
            return

        logger.debug('Running cc_embed xform')

        subrefname = nodes.fully_normalize_name(
                settings.cc_license_substitution_reference)
        subrefid = nodes.make_id(subrefname)
        subrefpath = nodes.fully_normalize_name(
                settings.cc_license_location)

        subreflist = self.document.traverse(nodes.substitution_reference)
        if subrefname not in subreflist:
            subrefloc = self.find_location(subrefpath)

            # append sub. ref. at location
            subrefnode = nodes.substitution_reference(None, None,
                    refname=subrefname)
            subrefloc.append(subrefnode)
            self.document.note_substitution_ref(subrefnode, subrefname)

        license = self.generate_cc_license()
        # append sub. def. to document
        subdefnode = nodes.substitution_definition(names=subrefname)
        subdefnode.append(license)
        self.document.append(subdefnode)
        self.document.note_substitution_def(subdefnode, subrefname)
Esempio n. 4
0
    def apply(self):
        if not hasattr(self.document.settings, 'breadcrumb') \
                or not getattr(self.document.settings, 'breadcrumb', None):
            return

        logger.debug('Running breadcrumb xform')

        subrefname = nodes.fully_normalize_name(
            self.document.settings.breadcrumb_substitution_reference)
        subrefid = nodes.make_id(subrefname)

        subreflist = self.document.traverse(nodes.substitution_reference)
        
        if subrefname not in subreflist:
            subloc = self.find_breadcrumb_location()

            # append sub. reference at location
            subrefnode = nodes.substitution_reference(None, subrefname)
            subrefnode['refname'] = subrefname
            subloc.append(subrefnode)
            self.document.note_substitution_ref(subrefnode, subrefname)

        breadcrumb = self.generate_breadcrumb()
        # append sub. definition to document
        subdefnode = nodes.substitution_definition()
        subdefnode.append(breadcrumb)
        subdefnode['names'].append(subrefname)
        self.document.append(subdefnode)
        self.document.note_substitution_def(subdefnode, subrefname) 
 def run(self):
     """Execute the directive parsing."""
     node = BlogNode()
     for key in self.option_spec:
         if key in self.options:
             node[key] = self.options[key]
         else:
             node[key] = self.empty_defaults[key]
     # Extract some time information:
     time = datetime.strptime(node['time'], self.date_format)
     year = time.year
     node['short_time'] = time.strftime(self.short_date_format)
     node['time'] = time
     node['category_ref'] = None
     node['docname'] = None
     node['tags_ref'] = [None for _ in node['tags']]
     node['tags_and_ref'] = [(None, None) for _ in node['tags']]
     node['year'] = year
     # Store information about the node, for further processing in
     # other directives.
     return_nodes = []
     try:
         env = self.state.document.settings.env
         targetid = "post-%d" % env.new_serialno('post')
         targetnode = nodes.target('', '', ids=[targetid])
         node['docname'] = env.docname
         node['dirname'] = os.path.dirname(env.docname)
         node['targetid'] = targetid
         if node['summary_image']:
             sub = nodes.substitution_definition()
             img = nodes.image()
             img['uri'] = node['summary_image']
             env.images.add_file('', img['uri'])
             sub += img
             return_nodes.append(sub)
         if not hasattr(env, 'all_posts'):
             env.all_posts = []
         env.all_posts.append({
             'time': time,
             'post_node': node.deepcopy(),
             'docname': env.docname,
             'datetime': time,
             'targetid': targetid,
             'targetnode': targetnode,
         })
         return_nodes.append(targetnode)
         return_nodes.append(node)
         return return_nodes
     except AttributeError:
         pass
     return [node]