def rst_bib(filestr, citations, pubfile, pubdata, numbering=True): """ Replace doconce citations and bibliography with reST syntax. If numbering is True, the keys used in the bibliography are replaced by numbers (RefX). This will often look better. """ if not citations: return filestr filestr = cite_with_multiple_args2multiple_cites(filestr) if numbering: # Find max no of digits n = len(str(max(citations.values()))) cite = '[Ref%%0%dd]' % n # cannot have blanks in ref label for label in citations: if numbering: filestr = filestr.replace('cite{%s}' % label, cite % citations[label] + '_') else: filestr = filestr.replace('cite{%s}' % label, '[%s]_' % label) if pubfile is not None: bibtext = bibliography(pubdata, citations, format='rst') if numbering: for label in citations: bibtext = bibtext.replace('[%s]' % label, cite % citations[label]) filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) return filestr
def rst_bib(filestr, citations, pubfile, pubdata, numbering=True): """ Replace doconce citations and bibliography with reST syntax. If numbering is True, the keys used in the bibliography are replaced by numbers (RefX). This will often look better. """ if not citations: return filestr filestr = cite_with_multiple_args2multiple_cites(filestr) if numbering: # Find max no of digits n = len(str(max(citations.values()))) cite = "[Ref%%0%dd]" % n # cannot have blanks in ref label for label in citations: if numbering: filestr = filestr.replace("cite{%s}" % label, cite % citations[label] + "_") else: filestr = filestr.replace("cite{%s}" % label, "[%s]_" % label) if pubfile is not None: # Could use rst format, but we stick to the common doconce format bibtext = bibliography(pubdata, citations, format="rst") if numbering: for label in citations: bibtext = bibtext.replace("[%s]" % label, cite % citations[label]) filestr = re.sub(r"^BIBFILE:.+$", bibtext, filestr, flags=re.MULTILINE) return filestr
def rst_bib(filestr, citations, pubfile, pubdata, numbering=True): """ Replace doconce citations and bibliography with reST syntax. If numbering is True, the keys used in the bibliography are replaced by numbers (RefX). This will often look better. """ if not citations: return filestr filestr = cite_with_multiple_args2multiple_cites(filestr) if numbering: # Find max no of digits n = len(str(max(citations.values()))) cite = '[Ref%%0%dd]' % n # cannot have blanks in ref label for label in citations: if numbering: filestr = filestr.replace('cite{%s}' % label, cite % citations[label] + '_') else: filestr = filestr.replace('cite{%s}' % label, '[%s]_' % label) if pubfile is not None: # Could use rst format, but we stick to the common doconce format bibtext = bibliography(pubdata, citations, format='rst') if numbering: for label in citations: bibtext = bibtext.replace('[%s]' % label, cite % citations[label]) filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) return filestr
def ipynb_index_bib(filestr, index, citations, pubfile, pubdata): # ipynb has support for latex-style bibliography. # Quite some code here is copy from latex_index_bib # http://nbviewer.ipython.org/github/ipython/nbconvert-examples/blob/master/citations/Tutorial.ipynb if citations: from common import cite_with_multiple_args2multiple_cites filestr = cite_with_multiple_args2multiple_cites(filestr) for label in citations: filestr = filestr.replace( 'cite{%s}' % label, '<cite data-cite="%s">[%d]</cite>' % (label, citations[label])) if pubfile is not None: # Always produce a new bibtex file bibtexfile = pubfile[:-3] + 'bib' errwarn('\nexporting publish database %s to %s:' % (pubfile, bibtexfile)) publish_cmd = 'publish export %s' % os.path.basename(bibtexfile) # Note: we have to run publish in the directory where pubfile resides this_dir = os.getcwd() pubfile_dir = os.path.dirname(pubfile) if not pubfile_dir: pubfile_dir = os.curdir os.chdir(pubfile_dir) os.system(publish_cmd) os.chdir(this_dir) bibstyle = option('latex_bibstyle=', 'plain') from latex import fix_latex_command_regex bibtext = fix_latex_command_regex(r""" ((*- extends 'latex_article.tplx' -*)) ((* block bibliography *)) \bibliographystyle{%s} \bibliography{%s} ((* endblock bibliography *)) """ % (bibstyle, bibtexfile[:-4]), application='replacement') filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) # Save idx{} and label{} as metadata, also have labels as div tags filestr = re.sub(r'((idx\{.+?\})', r'<!-- dom:\g<1> -->', filestr) filestr = re.sub(r'(label\{(.+?)\})', r'<!-- dom:\g<1> --><div id="\g<2>"></div>', filestr) # Also treat special cell delimiter comments that might appear from # doconce ipynb2doconce conversions filestr = re.sub(r'^# ---------- (markdown|code) cell$', '', filestr, flags=re.MULTILINE) return filestr
def rst_bib(filestr, citations, pubfile, pubdata, numbering=True): """ Replace doconce citations and bibliography with reST syntax. If numbering is True, the keys used in the bibliography are replaced by numbers (RefX). This will often look better. """ if not citations: return filestr filestr = cite_with_multiple_args2multiple_cites(filestr) if numbering: # Find max no of digits n = len(str(max(citations.values()))) cite = '[Ref%%0%dd]' % n # cannot have blanks in ref label for label in citations: if numbering: filestr = filestr.replace('cite{%s}' % label, cite % citations[label] + '_') else: filestr = filestr.replace('cite{%s}' % label, '[%s]_' % label) if pubfile is not None: # Could use rst format, but we stick to the common doconce format bibtext = bibliography(pubdata, citations, format='rst') if numbering: for label in citations: try: bibtext = bibtext.replace( '[%s]' % label, cite % citations[label]) except UnicodeDecodeError as e: if "can't decode byte" in str(e): try: bibtext = bibtext.decode('utf-8').replace( '[%s]' % label, cite % citations[label]) except UnicodeDecodeError as e: errwarn('UnicodeDecodeError: ' + e) errwarn('*** error: problems in %s' % pubfile) errwarn(' with key ' + label) errwarn(' tried to do decode("utf-8"), but it did not work') else: errwarn(e) errwarn('*** error: problems in %s' % pubfile) errwarn(' with key ' + label) _abort() filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) return filestr
def ipynb_index_bib(filestr, index, citations, pubfile, pubdata): # ipynb has support for latex-style bibliography. # Quite some code here is copy from latex_index_bib # http://nbviewer.ipython.org/github/ipython/nbconvert-examples/blob/master/citations/Tutorial.ipynb if citations: from common import cite_with_multiple_args2multiple_cites filestr = cite_with_multiple_args2multiple_cites(filestr) for label in citations: filestr = filestr.replace('cite{%s}' % label, '<cite data-cite="%s">[%d]</cite>' % (label, citations[label])) if pubfile is not None: # Always produce a new bibtex file bibtexfile = pubfile[:-3] + 'bib' errwarn('\nexporting publish database %s to %s:' % (pubfile, bibtexfile)) publish_cmd = 'publish export %s' % os.path.basename(bibtexfile) # Note: we have to run publish in the directory where pubfile resides this_dir = os.getcwd() pubfile_dir = os.path.dirname(pubfile) if not pubfile_dir: pubfile_dir = os.curdir os.chdir(pubfile_dir) os.system(publish_cmd) os.chdir(this_dir) bibstyle = option('latex_bibstyle=', 'plain') from latex import fix_latex_command_regex bibtext = fix_latex_command_regex(r""" ((*- extends 'latex_article.tplx' -*)) ((* block bibliography *)) \bibliographystyle{%s} \bibliography{%s} ((* endblock bibliography *)) """ % (bibstyle, bibtexfile[:-4]), application='replacement') filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) # Save idx{} and label{} as metadata, also have labels as div tags filestr = re.sub(r'((idx\{.+?\})', r'<!-- dom:\g<1> -->', filestr) filestr = re.sub(r'(label\{(.+?)\})', r'<!-- dom:\g<1> --><div id="\g<2>"></div>', filestr) # Also treat special cell delimiter comments that might appear from # doconce ipynb2doconce conversions filestr = re.sub(r'^# ---------- (markdown|code) cell$', '', filestr, flags=re.MULTILINE) return filestr
def plain_index_bib(filestr, index, citations, pubfile, pubdata): if citations: filestr = cite_with_multiple_args2multiple_cites(filestr) for label in citations: filestr = filestr.replace('cite{%s}' % label, '[%d]' % citations[label]) if pubfile is not None: bibtext = bibliography(pubdata, citations, format='doconce') bibtext = re.sub(r'label\{.+?\} ', '', bibtext) bibtext = bibtext.replace('_', '') filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) # remove all index entries: filestr = re.sub(r'idx\{.+?\}\n?', '', filestr) # no index since line numbers from the .do.txt (in index dict) # never correspond to the output format file #filestr += '\n\n======= Index =======\n\n' #for word in index: # filestr + = '%s, line %s\n' % (word, ', '.join(index[word])) return filestr
def plain_index_bib(filestr, index, citations, pubfile, pubdata): if citations: filestr = cite_with_multiple_args2multiple_cites(filestr) for label in citations: filestr = filestr.replace("cite{%s}" % label, "[%d]" % citations[label]) if pubfile is not None: bibtext = bibliography(pubdata, citations, format="doconce") bibtext = re.sub(r"label\{.+?\} ", "", bibtext) # Remove boldface _author_ (typically 12. _John Doe and Jane Doe_.) bibtext = re.sub(r"(\d+)\. _(.+)_\.", "\g<2>", bibtext) filestr = re.sub(r"^BIBFILE:.+$", bibtext, filestr, flags=re.MULTILINE) # remove all index entries: filestr = re.sub(r"idx\{.+?\}\n?", "", filestr) # no index since line numbers from the .do.txt (in index dict) # never correspond to the output format file # filestr += '\n\n======= Index =======\n\n' # for word in index: # filestr + = '%s, line %s\n' % (word, ', '.join(index[word])) return filestr
def xml_index_bib(filestr, index, citations, pubfile, pubdata): if citations: filestr = cite_with_multiple_args2multiple_cites(filestr) for label in citations: filestr = filestr.replace('cite{%s}' % label, '<cite label="%s">%s</cite>' % \ (label, citations[label])) if pubfile is not None: bibtext = bibliography(pubdata, citations, format='xml') bibtext = """ <!-- begin bibliography --> %s <!-- end bibliography --> """ % bibtext filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) filestr = re.sub(r'idx\{(.+?)\}\n?', r'<index>\g<1></index>', filestr) return filestr