1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
from .base import PwebFormatter
from .tex import PwebTexPygmentsFormatter
from subprocess import Popen, PIPE
import base64
import sys
import os
import io
import html
from nbconvert import filters
class PwebHTMLFormatter(PwebFormatter):
def preformat_chunk(self, chunk):
if chunk["type"] == "doc":
return chunk
from pygments import highlight
from IPython.lib.lexers import IPyLexer
#from pygments.lexers import PythonLexer, PythonConsoleLexer, TextLexer
from pygments.formatters import HtmlFormatter
chunk['content'] = highlight(chunk['content'], IPyLexer(), HtmlFormatter())
return chunk
def initformat(self):
self.formatdict = dict(codestart='',
codeend='',
outputstart='\n<div class="highlight"><pre>',
outputend='</pre></div>\n',
figfmt='.png',
width='600',
doctype='html')
self.fig_mimetypes = ["application/svg+xml", "image/png", "image/jpg"]
self.mimetypes = ["text/html", "application/javascript"]
self.file_ext = "html"
def escape(self, text):
return html.escape(text)
def highlight_ansi_and_escape(self, text):
return filters.ansi2html(text)
def formatfigure(self, chunk):
result = ""
figstring = ""
for fig in chunk['figure']:
figstring += ('<img src="%s" width="%s"/>\n' % (fig, chunk['width']))
# Figure environment
if chunk['caption']:
# Write labels as data-attribute for javascript etc.
if chunk['name']:
labelstring = 'data-label = "fig:%s"' % chunk["name"]
else:
labelstring = ""
result += ("<figure>\n" \
"%s"
"<figcaption %s>%s</figcaption>\n</figure>" % (figstring, labelstring, chunk['caption']))
else:
result += figstring
return result
class PwebMDtoHTMLFormatter(PwebHTMLFormatter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
from .templates import htmltemplate
from .. import themes
from pygments.formatters import HtmlFormatter
from .. import __version__
import time
self.mimetypes = ["text/html", "text/markdown", "application/javascript"]
theme_css = ""
try:
theme_css += getattr(themes, self.theme)
except:
print("Can't find requested theme. Using Skeleton")
theme_css += getattr(themes, "skeleton")
self.header = (htmltemplate["header"] %
{"pygments_css" : HtmlFormatter().get_style_defs(),
"theme_css" : theme_css})
self.footer = (htmltemplate["footer"] %
{"source": self.source, "version": __version__,
"time": time.strftime("%d-%m-%Y", time.localtime())})
def parsetitle(self, chunk):
"""Parse titleblock from first doc chunk, like Pandoc"""
lines = chunk['content'].splitlines()
if len(lines) > 3:
if lines[0].startswith("%"):
lines[0] = '<H1 class = "title">%s</H1>' % (lines[0].replace("%", "", ))
if lines[1].startswith("%"):
lines[1] = '<strong>Author:</strong> %s <BR/>' % (lines[1].replace("%", "", ))
if lines[2].startswith("%"):
lines[2] = '<strong>Date:</strong> %s <BR/>' % (lines[2].replace("%", "", ))
chunk['content'] = "\n".join(lines)
return chunk
def format_docchunk(self, chunk):
if 'number' in chunk and chunk['number'] == 1:
chunk = self.parsetitle(chunk)
try:
import markdown
except ImportError:
message = "You'll need to install python markdown in order to use markdown to html formatter\nrun 'pip install markdown' to install"
print(message)
return message # was returning None, which was passed to join method
from .markdownmath import MathExtension
chunk["content"] = markdown.markdown(chunk["content"], extensions=[MathExtension()])
return chunk['content']
def formatfigure(self, chunk):
result = ""
figstring = ""
for fig in chunk['figure']:
fh = open(os.path.join(self.wd, fig), "rb")
bfig = fh.read()
fh.close()
fig_base64 = base64.b64encode(bfig).decode("utf-8")
figstring += ('<img src="data:image/png;base64,%s" width="%s"/>\n' % (fig_base64, chunk['width']))
# Figure environment
if chunk['caption']:
# Write labels as data-attribute for javascript etc.
if chunk['name']:
labelstring = 'data-label = "fig:%s"' % chunk["name"]
else:
labelstring = ""
result += ("<figure>\n" \
"%s"
"<figcaption %s>%s</figcaption>\n</figure>" % (figstring, labelstring, chunk['caption']))
else:
result += figstring
return result
class PwebPandocMDtoHTMLFormatter(PwebMDtoHTMLFormatter):
def format_docchunk(self, chunk):
if 'number' in chunk and chunk['number'] == 1:
chunk = self.parsetitle(chunk)
try:
pandoc = Popen(["pandoc", "--mathjax", "-t", "html", "-f", "markdown"], stdin=PIPE, stdout=PIPE)
except:
sys.stderr.write("ERROR: Can't find pandoc")
raise
pandoc.stdin.write(chunk['content'].encode('utf-8'))
chunk['content'] = pandoc.communicate()[0].decode('utf-8')
return chunk['content']
class PwebPandoctoTexFormatter(PwebTexPygmentsFormatter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
from pygments.formatters import LatexFormatter
self.header = ("""\\documentclass[a4paper,11pt,final]{article}
\\usepackage{fancyvrb, color, graphicx, hyperref, amsmath, url, textcomp}
\\usepackage{palatino}
\\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}
%%Set different options for xetex and luatex
\\usepackage{iftex}
\\ifxetex\\usepackage{fontspec}\\fi
\\ifluatex\\usepackage{fontspec}\\fi
\\usepackage{xcolor}
%% ANSI colors from nbconvert
\\definecolor{ansi-black}{HTML}{3E424D}
\\definecolor{ansi-black-intense}{HTML}{282C36}
\\definecolor{ansi-red}{HTML}{E75C58}
\\definecolor{ansi-red-intense}{HTML}{B22B31}
\\definecolor{ansi-green}{HTML}{00A250}
\\definecolor{ansi-green-intense}{HTML}{007427}
\\definecolor{ansi-yellow}{HTML}{DDB62B}
\\definecolor{ansi-yellow-intense}{HTML}{B27D12}
\\definecolor{ansi-blue}{HTML}{208FFB}
\\definecolor{ansi-blue-intense}{HTML}{0065CA}
\\definecolor{ansi-magenta}{HTML}{D160C4}
\\definecolor{ansi-magenta-intense}{HTML}{A03196}
\\definecolor{ansi-cyan}{HTML}{60C6C8}
\\definecolor{ansi-cyan-intense}{HTML}{258F8F}
\\definecolor{ansi-white}{HTML}{C5C1B4}
\\definecolor{ansi-white-intense}{HTML}{A1A6B2}
\\hypersetup
{ pdfauthor = {Pweave},
pdftitle={Published from %s},
colorlinks=TRUE,
linkcolor=black,
citecolor=blue,
urlcolor=blue
}
\\setlength{\parindent}{0pt}
\\setlength{\parskip}{1.2ex}
%% fix for pandoc 1.14
\\providecommand{\\tightlist}{%%
\\setlength{\\itemsep}{0pt}\\setlength{\\parskip}{0pt}}
%s
""") % (self.source, LatexFormatter().get_style_defs())
self.footer = r"\end{document}"
self.subheader = "\n\\begin{document}\n"
self.fig_mimetypes = ["application/pdf", "image/png", "image/jpg"]
def add_header(self):
"""Can be used to add header to self.formatted list"""
self.formatted = self.header + self.subheader + self.formatted
def parsetitle(self, chunk):
"""Parse titleblock from first doc chunk, like Pandoc"""
lines = chunk['content'].splitlines()
if len(lines) > 3:
if lines[0].startswith("%"):
self.header += '\n\\title{%s}\n' % (lines[0].replace("%", "", ))
lines[0] = ""
if lines[1].startswith("%"):
self.header += '\\author{%s}\n' % (lines[1].replace("%", "", ))
lines[1] = ""
if lines[2].startswith("%"):
self.header += '\\date{%s}\n' % (lines[2].replace("%", "", ))
lines[2] = ""
self.subheader += "\maketitle\n"
chunk['content'] = "\n".join(lines)
return chunk
def format_docchunk(self, chunk):
if 'number' in chunk and chunk['number'] == 1:
chunk = self.parsetitle(chunk)
try:
pandoc = Popen(["pandoc", "-t", "latex+raw_tex", "-f", "markdown"], stdin=PIPE, stdout=PIPE)
except:
sys.stderr.write("ERROR: Can't find pandoc")
raise
pandoc.stdin.write(chunk['content'].encode('utf-8'))
chunk['content'] = pandoc.communicate()[0].decode('utf-8')
return chunk['content']
|