Issue #886 resolved

Sections inside only:: tags do not show up in the html

created an issue

Please try to compile the attached example using: make html

Comments (15)

  1. mateobur reporter

    Thanks for the reply Jon,

    But if you see the attached document, for example example2.rst

    bla bla
    .. only:: randomtag
        blah blah

    To me, it seems like 'hello' is the main document heading and the sections with "==" are secondary, but still, the second section isn't appearing in the document.

    Sorry if this is me misunderstanding something.

  2. Jon Waltman

    Instead of the main document heading, I think it is actually the current heading.

    That is, only sub-headings are handled correctly.

    So this will work:

    bla bla
    .. only:: randomtag
        blah blah

    because the "--" heading is lower than "==".

  3. mateobur reporter

    Thanks again for you attention,

    But then, this is not completely useful because 'lacabr' will have to be a subsection of 'example' and will appear as such in the document index, what I really want is to have a same-level section only when I apply the tag. Maybe that's just not possible, but seems odd.

  4. Jon Waltman

    Yea, but it does avoid some ambiguous situations like the following:

    Section A
    bla bla
    .. only:: randomtag
        Section B
        blah blah
    Subsection A.1 or B.1
    blah blah
  5. mateobur reporter

    If it has the same indentation level as B, it will be B.1, in your example A.1 :). It can't be a subsection of B in your example because B may not be there in the final document. Odd thing is, I have some documents where the same-level headers inside only:: tag work for some of the tags and not for others.

    PD: I'm hanging around the #pocoo IRC channel if you want to chat directly.

  6. Jon Waltman

    I hacked together a new only directive that should work with nested and same-level headers. You try it out by adding the following to your

    from docutils.parsers.rst import Directive
    class ReallyOnly(Directive):
        Directive to only include text if the given tag(s) are enabled.
        has_content = True
        required_arguments = 1
        optional_arguments = 0
        final_argument_whitespace = True
        option_spec = {}
        def run(self):
            from docutils import nodes
            from sphinx import addnodes
            from sphinx.util.nodes import set_source_info
            node = addnodes.only()
            node.document = self.state.document
            set_source_info(self, node)
            node['expr'] = self.arguments[0]
            # hack around title style bookkeeping
            surrounding_title_styles = self.state.memo.title_styles
            surrounding_section_level = self.state.memo.section_level
            self.state.memo.title_styles = []
            self.state.memo.section_level = 0
                result = self.state.nested_parse(self.content, 0, node,
                depth = len(surrounding_title_styles)
                if self.state.memo.title_styles:
                    style = self.state.memo.title_styles[0]
                    if style in surrounding_title_styles:
                        depth = surrounding_title_styles.index(style)
                parent = self.state.parent
                for i in xrange(len(surrounding_title_styles) - depth):
                    if parent.parent:
                        parent = parent.parent
                self.state.memo.title_styles = surrounding_title_styles
                self.state.memo.section_level = surrounding_section_level
            return []
    def setup(app):
        # from sphinx.directives import Only
        app.add_directive('only', ReallyOnly)
        # # pretty print doctree
        # def dump_doctree(app, doctree, docname):
        #     print '\n# %s\n%s\n' % (docname, doctree.pformat())
        # app.connect('doctree-resolved', dump_doctree)
  7. Log in to comment