def pandoc_ref_and_label(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # Replace all references to sections. Pandoc needs a coding of # the section header as link. (Not using this anymore.) def title2pandoc(title): # http://johnmacfarlane.net/pandoc/README.html for c in ("?", ";", ":"): title = title.replace(c, "") title = title.replace(" ", "-").strip() start = 0 for i in range(len(title)): if title[i].isalpha(): start = i title = title[start:] title = title.lower() if not title: title = "section" return title for label in section_label2title: filestr = filestr.replace("ref{%s}" % label, "[%s](#%s)" % (section_label2title[label], label)) # title2pandoc(section_label2title[label]))) # Treat the remaining ref{} # Note done. # Can place labels in equations as <a name tags before the equation # environment and create appropriate [link](#label) syntax. # Need to specify figures and movies separately to find the # right link text. html.py has probably most solutions. filestr = re.sub(r"([Ff]igure|[Mm]ovie)\s+ref\{(.+?)\}", "[\g<1>](#\g<2>)", filestr) # Remaining ref{} (should protect \eqref) filestr = re.sub(r"ref\{(.+?)\}", "[\g<1>](#\g<1>)", filestr) # filestr = re.sub(r'([^q])ref\{(.+?)\}', '\g<1>[\g<2>](#\g<2>)', filestr) return filestr
def wiki_ref_and_label_common(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # remove label{...} from output filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining # anchors in titles do not work... # replace all references to sections: for label in section_label2title: title = section_label2title[label] filestr = filestr.replace('ref{%s}' % label, '[#%s]' % title.replace(' ', '_')) from common import ref2equations filestr = ref2equations(filestr) # replace remaining ref{x} as x filestr = re.sub(r'ref\{(.+?)\}', '\g<1>', filestr) return filestr
def plain_ref_and_label(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # remove label{...} from output (when only label{} on a line, remove # the newline too, leave label in figure captions, and remove all the rest) #filestr = re.sub(r'^label\{.+?\}\s*$', '', filestr, flags=re.MULTILINE) cpattern = re.compile(r'^label\{.+?\}\s*$', flags=re.MULTILINE) filestr = cpattern.sub('', filestr) #filestr = re.sub(r'^(FIGURE:.+)label\{(.+?)\}', '\g<1>{\g<2>}', filestr, flags=re.MULTILINE) cpattern = re.compile(r'^(FIGURE:.+)label\{(.+?)\}', flags=re.MULTILINE) filestr = cpattern.sub('\g<1>{\g<2>}', filestr) filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining # replace all references to sections: for label in section_label2title: filestr = filestr.replace('ref{%s}' % label, '"%s"' % section_label2title[label]) from common import ref2equations filestr = ref2equations(filestr) return filestr
def matlabnb_ref_and_label(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # remove label{...} from output (when only label{} on a line, remove # the newline too, leave label in figure captions, and remove all the rest) #filestr = re.sub(r'^label\{.+?\}\s*$', '', filestr, flags=re.MULTILINE) cpattern = re.compile(r'^label\{.+?\}\s*$', flags=re.MULTILINE) filestr = cpattern.sub('', filestr) #filestr = re.sub(r'^(FIGURE:.+)label\{(.+?)\}', '\g<1>{\g<2>}', filestr, flags=re.MULTILINE) cpattern = re.compile(r'^(FIGURE:.+)label\{(.+?)\}', flags=re.MULTILINE) filestr = cpattern.sub('\g<1>{\g<2>}', filestr) filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining # replace all references to sections: for label in section_label2title: filestr = filestr.replace('ref{%s}' % label, '"%s"' % section_label2title[label]) from common import ref2equations filestr = ref2equations(filestr) return filestr
def pandoc_ref_and_label(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # Replace all references to sections. Pandoc needs a coding of # the section header as link. (Not using this anymore.) def title2pandoc(title): # http://johnmacfarlane.net/pandoc/README.html for c in ('?', ';', ':'): title = title.replace(c, '') title = title.replace(' ', '-').strip() start = 0 for i in range(len(title)): if title[i].isalpha(): start = i title = title[start:] title = title.lower() if not title: title = 'section' return title for label in section_label2title: filestr = filestr.replace( 'ref{%s}' % label, '[%s](#%s)' % (section_label2title[label], label)) # title2pandoc(section_label2title[label]))) # Treat the remaining ref{} # Note done. # Can place labels in equations as <a name tags before the equation # environment and create appropriate [link](#label) syntax. # Need to specify figures and movies separately to find the # right link text. html.py has probably most solutions. filestr = re.sub(r'([Ff]igure|[Mm]ovie)\s+ref\{(.+?)\}', '[\g<1>](#\g<2>)', filestr) # Remaining ref{} (should protect \eqref) filestr = re.sub(r'ref\{(.+?)\}', '[\g<1>](#\g<1>)', filestr) #filestr = re.sub(r'([^q])ref\{(.+?)\}', '\g<1>[\g<2>](#\g<2>)', filestr) return filestr
def ref_and_label_commoncode(section_label2title, format, filestr): filestr = fix_ref_section_chapter(filestr, format) # Deal with the problem of identical titles, which makes problem # with non-unique links in reST: add a counter to the title debugtext = '' section_pattern = r'^\s*(={3,9})(.+?)(={3,9})(\s*label\{(.+?)\})?' all_sections = re.findall(section_pattern, filestr, flags=re.MULTILINE) # First count the no of titles with the same wording titles = {} max_heading = 1 # track the top heading level for correct TITLE typesetting for heading, title, dummy2, dummy3, label in all_sections: entry = None if label == '' else label if title in titles: titles[title].append(entry) else: titles[title] = [entry] max_heading = max(max_heading, len(heading)) # Typeset TITLE so that it gets the highest+1 (but no higher) section sevel max_heading += 2 # one level up (2 =) max_heading = min(max_heading, 9) pattern = r'^TITLE:\s*(.+)$' if format == 'sphinx': # Title cannot be more than 63 chars... m = re.search(pattern, filestr, flags=re.MULTILINE) if m: title = m.group(1).strip() if len(title) > 63: errwarn('*** error: sphinx title cannot be longer than 63 characters') errwarn(' current title: "%s" (%d characters)' % (title, len(title))) _abort() filestr = re.sub(pattern, '.. Document title:\n\n%s \g<1> %s\n' % ('='*max_heading, '='*max_heading), filestr, flags=re.MULTILINE) # Make new titles title_counter = {} # count repeated titles (need to append counter to make unique links) sections = [] for heading, title, dummy2, dummy3, label in all_sections: label = None if label == '' else label if len(titles[title]) > 1: if title in title_counter: title_counter[title] += 1 else: title_counter[title] = 1 # Add much whitespace so we can recognize the titles after # formats are compiled and remove the number new_title = title + ' (%d) ' % title_counter[title] sections.append((heading, new_title, label, title)) if label in section_label2title: section_label2title[label] = new_title else: sections.append((heading, title, label, title)) # Make replacements for heading, title, label, old_title in sections: if title != old_title: debugtext += '\nchanged title: %s -> %s\n' % (old_title, title) # Avoid trouble with \t, \n in replacement title = title.replace('\\', '\\\\') # The substitution depends on whether we have a label or not if label is not None: title_pattern = r'%s\s*%s\s*%s\s*label\{%s\}' % (heading, re.escape(old_title), heading, label) # title may contain ? () etc., that's why we take re.escape replacement = '.. _%s:\n\n' % label + r'%s %s %s' % \ (heading, title, heading) else: title_pattern = r'%s\s*%s\s*%s' % (heading, re.escape(old_title), heading) replacement = r'%s %s %s' % (heading, title, heading) filestr, n = re.subn(title_pattern, replacement, filestr, count=1) if n > 1: raise ValueError('Replaced more than one title. BUG!') # remove label{...} from output #filestr = re.sub(r'^label\{.+?\}\s*$', '', filestr, flags=re.MULTILINE) cpattern = re.compile(r'^label\{[^}]+?\}\s*$', flags=re.MULTILINE) filestr = cpattern.sub('', filestr) filestr = re.sub(r'label\{[^}]+?\}', '', filestr) # all the remaining import doconce doconce.debugpr(debugtext) return filestr