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

mateobur avatarmateobur created an issue

Please try to compile the attached example using: make html

Comments (13)

  1. mateobur

    Thanks for the reply Jon,

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

    hello
    *****
    
    example
    =======
    
    bla bla
    
    .. only:: randomtag
    
        lacabr
        ======
    
        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:

    hello
    *****
    
    example
    =======
    
    bla bla
    
    .. only:: randomtag
    
        lacabr
        ------
    
        blah blah
    

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

  3. mateobur

    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:

    top
    ***
    
    Section A
    =========
    
    bla bla
    
    .. only:: randomtag
    
        Section B
        =========
    
        blah blah
    
    
    Subsection A.1 or B.1
    ---------------------
    
    blah blah
    
  5. mateobur

    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 conf.py

    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
            try:
                result = self.state.nested_parse(self.content, 0, node,
                                                 match_titles=1)
                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
                parent.append(node)
            finally:
                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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.