Hello group,

I'm trying to output references differently under an html build and a latexpdf build.

Something like this:

rst text:

"
..  my_label:

My title
----------
"

I would like to have a reference in html like this:

"See section My title"

and in latex like this:

"See section 2.3"

I tried to use a custom node and a custom role "yref". This works with an html build but in the latex build, the files containing "yref" are disregarded and not included in the build. If I take the "yref" out of the files, they are processed as usual. The rest of my mail is a little bit long but I tried to write a minimal example of what I did.

What am I doing wrong? What is the correct way to handle custom references?

Thanks in advance for any help.

Have a nice day,

Nikolaj

--------------------------------------------------------------------------------------------------------------------------


This is what I did:

first I define a custom node:

class ortools_ref_node(nodes.Inline, nodes.Element):
    def __init__(self):
        super(ortools_ref_node, self).__init__()
        self.attributes['ortools_title'] = None
        self.attributes['ortools_html_target'] = None
        self.attributes['ortools_latex_target'] = None
        self.attributes['ortools_type'] = None
        self.attributes['capital'] = None

    #other things not mentioned here
    #...

then I define a custom role:

class ortools_ref_role(XRefRole):
    def __init__(self):
        XRefRole.__init__(self, fix_parens=False, lowercase=False)

    def result_nodes(self, document, env, node, is_ref):
        if not is_ref:
            return [node], []
        domain = env.domains['std']
        labels = env.app.config.labels
        newnode = ortools_ref_node()
        target = node['reftarget']
        newnode['reftype'] = node['reftype']
        newnode['refdoc'] = node['refdoc']
        newnode['reftarget'] = target
        if labels.has_key(target):
          if node['refexplicit']:
            title = node.astext()
            if title.startswith('^'):
              newnode['capital'] = True
              title = title[1:]
          else:
            title = labels[target]['title']
          newnode.attributes['ortools_title'] = title
reftarget = os.path.join(labels[target]['rel_path'], labels[target]['filename'][:-4])
          labelid = nodes.make_id(target)

html_reluri = relative_uri(node['refdoc'], reftarget) + '.html#' + str(labelid)
          newnode.attributes['ortools_html_target'] = html_reluri
        else:
          print "Target '" + target + "' is not defined!"
          return [node], []

        return [newnode], []

The setup function is defined by:

def setup(app):
    app.add_node(ortools_ref_node, html=(visit_ortools_ref_html, None),
                              latex=(visit_ortools_ref_latex, None))
    #...
    app.add_config_value('labels', {}, True)
    app.add_role('yref', ortools_role)
    app.connect("builder-inited", get_labels)

Finally:

def visit_ortools_ref_html(self, node):
    target = node.attributes['ortools_html_target']
    title = transform_html_title(node.attributes['ortools_title'])

self.body.append(u'%s' % prefix + ' <a class="reference internal" href="%s" '
                             %  target +
                             u'><em>%s' % title +
                             u'</em></a>')
    raise nodes.SkipNode

This seems to work...

But the latex version doesn't work even when completely empty:

def visit_ortools_ref_latex(self, node):
    raise nodes.SkipNode


--
You received this message because you are subscribed to the Google Groups 
"sphinx-dev" group.
To post to this group, send email to sphinx-dev@googlegroups.com.
To unsubscribe from this group, send email to 
sphinx-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en.

Reply via email to