def process_todoext_nodes(app, doctree, fromdocname): """ process_todoext_nodes """ if not app.config['todoext_include_todosext']: for node in doctree.traverse(todoext_node): node.parent.remove(node) # Replace all todoextlist nodes with a list of the collected todosext. # Augment each todoext with a backlink to the original location. env = app.builder.env if hasattr(env, "settings") and hasattr(env.settings, "language_code"): lang = env.settings.language_code else: lang = "en" orig_entry = TITLES[lang]["original entry"] todomes = TITLES[lang]["todomes"] allowed_tsort = {'date', 'prio', 'title', 'release', 'source'} if not hasattr(env, 'todoext_all_todosext'): env.todoext_all_todosext = [] for ilist, node in enumerate(doctree.traverse(todoextlist)): if 'ids' in node: node['ids'] = [] if not app.config['todoext_include_todosext']: node.replace_self([]) continue nbtodo = 0 fcost = 0 content = [] todotag = node["todotag"] tsort = node["todosort"] if tsort == '': tsort = 'source' if tsort not in allowed_tsort: raise ValueError("option sort must in {0}, '{1}' is not".format( allowed_tsort, tsort)) double_list = [(info.get('todo%s' % tsort, ''), info.get('todotitle', ''), info) for info in env.todoext_all_todosext] double_list.sort(key=lambda x: x[:2]) for n, todoext_info_ in enumerate(double_list): todoext_info = todoext_info_[2] if todoext_info["todotag"] != todotag: continue nbtodo += 1 fcost += todoext_info.get("todocost", 0.0) para = nodes.paragraph(classes=['todoext-source']) if app.config['todoext_link_only']: description = locale_('<<%s>>' % orig_entry) else: description = ( locale_(todomes) % (orig_entry, os.path.split( todoext_info['source'])[-1], todoext_info['lineno'])) desc1 = description[:description.find('<<')] desc2 = description[description.find('>>') + 2:] para += nodes.Text(desc1, desc1) # Create a reference newnode = nodes.reference('', '', internal=True) innernode = nodes.emphasis(locale_(orig_entry), locale_(orig_entry)) try: newnode['refuri'] = app.builder.get_relative_uri( fromdocname, todoext_info['docname']) try: newnode['refuri'] += '#' + todoext_info['target']['refid'] except Exception as e: raise KeyError("refid in not present in '{0}'".format( todoext_info['target'])) from e except NoUri: # ignore if no URI can be determined, e.g. for LaTeX output pass newnode.append(innernode) para += newnode para += nodes.Text(desc2, desc2) # (Recursively) resolve references in the todoext content todoext_entry = todoext_info.get('todoext_copy', None) if todoext_entry is None: todoext_entry = todoext_info['todoext'] todoext_entry["ids"] = ["index-todoext-%d-%d" % (ilist, n)] # it apparently requires an attributes ids if not hasattr(todoext_entry, "settings"): todoext_entry.settings = Values() todoext_entry.settings.env = env # If an exception happens here, see blog 2017-05-21 from the # documentation. env.resolve_references(todoext_entry, todoext_info['docname'], app.builder) # Insert into the todoextlist content.append(todoext_entry) content.append(para) if fcost > 0: cost = nodes.paragraph() lab = "{0} items, cost: {1}".format(nbtodo, fcost) cost += nodes.Text(lab) content.append(cost) else: cost = nodes.paragraph() lab = "{0} items".format(nbtodo) cost += nodes.Text(lab) content.append(cost) node.replace_self(content)
def run(self): """ builds the todo text """ sett = self.state.document.settings language_code = sett.language_code lineno = self.lineno env = self.state.document.settings.env if hasattr( self.state.document.settings, "env") else None docname = None if env is None else env.docname if docname is not None: docname = docname.replace("\\", "/").split("/")[-1] legend = "{0}:{1}".format(docname, lineno) else: legend = '' if not self.options.get('class'): self.options['class'] = ['admonition-todoext'] # link to issue issue = self.options.get('issue', "").strip() if issue is not None and len(issue) > 0: if hasattr(sett, "extlinks"): extlinks = sett.extlinks elif env is not None and hasattr(env.config, "extlinks"): extlinks = env.config.extlinks else: available = "\n".join(sorted(sett.__dict__.keys())) available2 = "\n".join( sorted(env.config.__dict__.keys())) if env is not None else "-" raise ValueError("extlinks (wih a key 'issue') is not defined in the documentation settings, available in sett\n{0}\nCONFIG\n{1}".format( available, available2)) if "issue" not in extlinks: raise KeyError("key 'issue' is not present in extlinks") url, label = extlinks["issue"] url = url % str(issue) lab = label.format(issue) linkin = nodes.reference(lab, locale_(lab), refuri=url) link = nodes.paragraph() link += linkin else: link = None # cost cost = self.options.get('cost', "").strip() if cost: try: fcost = float(cost) except ValueError: raise ValueError( "unable to convert cost '{0}' into float".format(cost)) else: fcost = 0.0 # priority prio = self.options.get('priority', "").strip() # hidden hidden = self.options.get('hidden', "false").strip().lower() in { 'true', '1', ''} # body (todoext,) = super(TodoExt, self).run() if isinstance(todoext, nodes.system_message): return [todoext] # link if link: todoext += link # title title = self.options.get('title', "").strip() todotag = self.options.get('tag', '').strip() if len(title) > 0: title = ": " + title # prefix prefix = TITLES[language_code]["todo"] tododate = self.options.get('date', "").strip() todorelease = self.options.get('release', "").strip() infos = [] if len(todotag) > 0: infos.append(todotag) if len(prio) > 0: infos.append('P=%s' % prio) if fcost > 0: if int(fcost) == fcost: infos.append('C=%d' % int(fcost)) else: infos.append('C=%1.1f' % fcost) if todorelease: infos.append('v{0}'.format(todorelease)) if tododate: infos.append(tododate) if infos: prefix += "({0})".format(" - ".join(infos)) # main node title = nodes.title(text=locale_(prefix + title)) todoext.insert(0, title) todoext['todotag'] = todotag todoext['todocost'] = fcost todoext['todoprio'] = prio todoext['todohidden'] = hidden todoext['tododate'] = tododate todoext['todorelease'] = todorelease todoext['todotitle'] = self.options.get('title', "").strip() set_source_info(self, todoext) if hidden: todoext['todoext_copy'] = todoext.deepcopy() todoext.clear() if env is not None: targetid = 'indextodoe-%s' % env.new_serialno('indextodoe') targetnode = nodes.target(legend, '', ids=[targetid]) self.state.add_target(targetid, '', targetnode, lineno) # index node index = self.options.get('index', None) if index is not None: indexnode = addnodes.index() indexnode['entries'] = ne = [] indexnode['inline'] = False set_source_info(self, indexnode) for entry in index.split(","): ne.extend(process_index_entry(entry, targetid)) else: indexnode = None else: targetnode = None indexnode = None return [a for a in [indexnode, targetnode, todoext] if a is not None]
def run(self): """ builds the todo text """ sett = self.state.document.settings language_code = sett.language_code lineno = self.lineno env = self.state.document.settings.env if hasattr( self.state.document.settings, "env") else None docname = None if env is None else env.docname if docname is not None: docname = docname.replace("\\", "/").split("/")[-1] legend = "{0}:{1}".format(docname, lineno) else: legend = '' if not self.options.get('class'): self.options['class'] = ['admonition-todoext'] # link to issue issue = self.options.get('issue', "").strip() if issue is not None and len(issue) > 0: if hasattr(sett, "extlinks"): extlinks = sett.extlinks elif env is not None and hasattr(env.config, "extlinks"): extlinks = env.config.extlinks else: available = "\n".join(sorted(sett.__dict__.keys())) available2 = "\n".join(sorted( env.config.__dict__.keys())) if env is not None else "-" mes = "extlinks (wih a key 'issue') is not defined in the documentation settings, available in sett\n{0}\nCONFIG\n{1}" raise ValueError(mes.format(available, available2)) if "issue" not in extlinks: raise KeyError("key 'issue' is not present in extlinks") url, label = extlinks["issue"] url = url % str(issue) lab = label.format(issue) linkin = nodes.reference(lab, locale_(lab), refuri=url) link = nodes.paragraph() link += linkin else: link = None # cost cost = self.options.get('cost', "").strip() if cost: try: fcost = float(cost) except ValueError: raise ValueError( "unable to convert cost '{0}' into float".format(cost)) else: fcost = 0.0 # priority prio = self.options.get('priority', "").strip() # hidden hidden = self.options.get( 'hidden', "false").strip().lower() in {'true', '1', ''} # body (todoext, ) = super(TodoExt, self).run() if isinstance(todoext, nodes.system_message): return [todoext] # link if link: todoext += link # title title = self.options.get('title', "").strip() todotag = self.options.get('tag', '').strip() if len(title) > 0: title = ": " + title # prefix prefix = TITLES[language_code]["todo"] tododate = self.options.get('date', "").strip() todorelease = self.options.get('release', "").strip() infos = [] if len(todotag) > 0: infos.append(todotag) if len(prio) > 0: infos.append('P=%s' % prio) if fcost > 0: if int(fcost) == fcost: infos.append('C=%d' % int(fcost)) else: infos.append('C=%1.1f' % fcost) if todorelease: infos.append('v{0}'.format(todorelease)) if tododate: infos.append(tododate) if infos: prefix += "({0})".format(" - ".join(infos)) # main node title = nodes.title(text=locale_(prefix + title)) todoext.insert(0, title) todoext['todotag'] = todotag todoext['todocost'] = fcost todoext['todoprio'] = prio todoext['todohidden'] = hidden todoext['tododate'] = tododate todoext['todorelease'] = todorelease todoext['todotitle'] = self.options.get('title', "").strip() set_source_info(self, todoext) if hidden: todoext['todoext_copy'] = todoext.deepcopy() todoext.clear() if env is not None: targetid = 'indextodoe-%s' % env.new_serialno('indextodoe') targetnode = nodes.target(legend, '', ids=[targetid]) set_source_info(self, targetnode) self.state.add_target(targetid, '', targetnode, lineno) # index node index = self.options.get('index', None) if index is not None: indexnode = addnodes.index() indexnode['entries'] = ne = [] indexnode['inline'] = False set_source_info(self, indexnode) for entry in index.split(","): ne.extend(process_index_entry(entry, targetid)) else: indexnode = None else: targetnode = None indexnode = None return [a for a in [indexnode, targetnode, todoext] if a is not None]
def process_todoext_nodes(app, doctree, fromdocname): """ process_todoext_nodes """ if not app.config['todoext_include_todosext']: for node in doctree.traverse(todoext_node): node.parent.remove(node) # Replace all todoextlist nodes with a list of the collected todosext. # Augment each todoext with a backlink to the original location. env = app.builder.env if hasattr(env, "settings") and hasattr(env.settings, "language_code"): lang = env.settings.language_code else: lang = "en" orig_entry = TITLES[lang]["original entry"] todomes = TITLES[lang]["todomes"] allowed_tsort = {'date', 'prio', 'title', 'release', 'source'} if not hasattr(env, 'todoext_all_todosext'): env.todoext_all_todosext = [] for ilist, node in enumerate(doctree.traverse(todoextlist)): if 'ids' in node: node['ids'] = [] if not app.config['todoext_include_todosext']: node.replace_self([]) continue nbtodo = 0 fcost = 0 content = [] todotag = node["todotag"] tsort = node["todosort"] if tsort == '': tsort = 'source' if tsort not in allowed_tsort: raise ValueError( "option sort must in {0}, '{1}' is not".format(allowed_tsort, tsort)) double_list = [(info.get('todo%s' % tsort, ''), info.get('todotitle', ''), info) for info in env.todoext_all_todosext] double_list.sort(key=lambda x: x[:2]) for n, todoext_info_ in enumerate(double_list): todoext_info = todoext_info_[2] if todoext_info["todotag"] != todotag: continue nbtodo += 1 fcost += todoext_info.get("todocost", 0.0) para = nodes.paragraph(classes=['todoext-source']) if app.config['todoext_link_only']: description = locale_('<<%s>>' % orig_entry) else: description = ( locale_(todomes) % (orig_entry, os.path.split(todoext_info['source'])[-1], todoext_info['lineno']) ) desc1 = description[:description.find('<<')] desc2 = description[description.find('>>') + 2:] para += nodes.Text(desc1, desc1) # Create a reference newnode = nodes.reference('', '', internal=True) innernode = nodes.emphasis( locale_(orig_entry), locale_(orig_entry)) try: newnode['refuri'] = app.builder.get_relative_uri( fromdocname, todoext_info['docname']) try: newnode['refuri'] += '#' + todoext_info['target']['refid'] except Exception as e: raise KeyError("refid in not present in '{0}'".format( todoext_info['target'])) from e except NoUri: # ignore if no URI can be determined, e.g. for LaTeX output pass newnode.append(innernode) para += newnode para += nodes.Text(desc2, desc2) # (Recursively) resolve references in the todoext content todoext_entry = todoext_info.get('todoext_copy', None) if todoext_entry is None: todoext_entry = todoext_info['todoext'] todoext_entry["ids"] = ["index-todoext-%d-%d" % (ilist, n)] # it apparently requires an attributes ids env.resolve_references(todoext_entry, todoext_info['docname'], app.builder) # Insert into the todoextlist content.append(todoext_entry) content.append(para) if fcost > 0: cost = nodes.paragraph() lab = "{0} items, cost: {1}".format(nbtodo, fcost) cost += nodes.Text(lab) content.append(cost) else: cost = nodes.paragraph() lab = "{0} items".format(nbtodo) cost += nodes.Text(lab) content.append(cost) node.replace_self(content)