Show More
@@ -147,7 +147,7 b' class Converter(object):' | |||||
147 | user_preamble = None |
|
147 | user_preamble = None | |
148 | output = str() |
|
148 | output = str() | |
149 | raw_as_verbatim = False |
|
149 | raw_as_verbatim = False | |
150 |
|
|
150 | ||
151 | def __init__(self, infile): |
|
151 | def __init__(self, infile): | |
152 | self.infile = infile |
|
152 | self.infile = infile | |
153 | self.infile_dir, infile_root = os.path.split(infile) |
|
153 | self.infile_dir, infile_root = os.path.split(infile) | |
@@ -459,9 +459,26 b' class ConverterRST(Converter):' | |||||
459 | return rst_directive('.. raw:: javascript', output.javascript) |
|
459 | return rst_directive('.. raw:: javascript', output.javascript) | |
460 |
|
460 | |||
461 |
|
461 | |||
|
462 | ||||
|
463 | ||||
|
464 | def highlight(src, lang='python'): | |||
|
465 | """Return a syntax-highlighted version of the input source. | |||
|
466 | """ | |||
|
467 | from pygments import highlight | |||
|
468 | from pygments.lexers import get_lexer_by_name | |||
|
469 | from pygments.formatters import HtmlFormatter | |||
|
470 | ||||
|
471 | lexer = get_lexer_by_name(lang, stripall=True) | |||
|
472 | return highlight(src, lexer, HtmlFormatter()) | |||
|
473 | ||||
|
474 | ||||
462 | class ConverterMarkdown(Converter): |
|
475 | class ConverterMarkdown(Converter): | |
463 | extension = 'md' |
|
476 | extension = 'md' | |
464 |
|
477 | |||
|
478 | def __init__(self, infile, highlight_source=False): | |||
|
479 | super(ConverterMarkdown, self).__init__(infile) | |||
|
480 | self.highlight_source = highlight_source | |||
|
481 | ||||
465 | @DocInherit |
|
482 | @DocInherit | |
466 | def render_heading(self, cell): |
|
483 | def render_heading(self, cell): | |
467 | return ['{0} {1}'.format('#'*cell.level, cell.source), ''] |
|
484 | return ['{0} {1}'.format('#'*cell.level, cell.source), ''] | |
@@ -473,7 +490,9 b' class ConverterMarkdown(Converter):' | |||||
473 | lines = [] |
|
490 | lines = [] | |
474 | #lines.append('----') |
|
491 | #lines.append('----') | |
475 | lines.extend(['*In[%s]:*' % cell.prompt_number, '']) |
|
492 | lines.extend(['*In[%s]:*' % cell.prompt_number, '']) | |
476 | lines.extend([indent(cell.input), '']) |
|
493 | src = highlight(cell.input) if self.highlight_source else \ | |
|
494 | indent(cell.input) | |||
|
495 | lines.extend([src, '']) | |||
477 | if cell.outputs: |
|
496 | if cell.outputs: | |
478 | lines.extend(['==>', '']) |
|
497 | lines.extend(['==>', '']) | |
479 | for output in cell.outputs: |
|
498 | for output in cell.outputs: | |
@@ -487,7 +506,6 b' class ConverterMarkdown(Converter):' | |||||
487 | @DocInherit |
|
506 | @DocInherit | |
488 | def render_markdown(self, cell): |
|
507 | def render_markdown(self, cell): | |
489 | return [cell.source, ''] |
|
508 | return [cell.source, ''] | |
490 | #return [markdown2rst(cell.source)] |
|
|||
491 |
|
509 | |||
492 | @DocInherit |
|
510 | @DocInherit | |
493 | def render_raw(self, cell): |
|
511 | def render_raw(self, cell): | |
@@ -499,14 +517,16 b' class ConverterMarkdown(Converter):' | |||||
499 | @DocInherit |
|
517 | @DocInherit | |
500 | def render_pyout(self, output): |
|
518 | def render_pyout(self, output): | |
501 | lines = [] |
|
519 | lines = [] | |
502 | #lines.extend(['*Out[%s]:*' % output.prompt_number, '']) |
|
520 | ||
|
521 | ## if 'text' in output: | |||
|
522 | ## lines.extend(['*Out[%s]:*' % output.prompt_number, '']) | |||
503 |
|
523 | |||
504 | # output is a dictionary like object with type as a key |
|
524 | # output is a dictionary like object with type as a key | |
505 | if 'latex' in output: |
|
525 | if 'latex' in output: | |
506 | pass |
|
526 | pass | |
507 |
|
527 | |||
508 | if 'text' in output: |
|
528 | if 'text' in output: | |
509 | lines.extend([indent(output.text)]) |
|
529 | lines.extend(['<pre>', indent(output.text), '</pre>']) | |
510 |
|
530 | |||
511 | lines.append('') |
|
531 | lines.append('') | |
512 | return lines |
|
532 | return lines | |
@@ -558,6 +578,11 b' class ConverterMarkdown(Converter):' | |||||
558 | return ['JavaScript:', indent(output.javascript)] |
|
578 | return ['JavaScript:', indent(output.javascript)] | |
559 |
|
579 | |||
560 |
|
580 | |||
|
581 | def return_list(x): | |||
|
582 | """Ensure that x is returned as a list or inside one""" | |||
|
583 | return x if isinstance(x, list) else [x] | |||
|
584 | ||||
|
585 | ||||
561 | class ConverterQuickHTML(Converter): |
|
586 | class ConverterQuickHTML(Converter): | |
562 | extension = 'html' |
|
587 | extension = 'html' | |
563 |
|
588 | |||
@@ -639,7 +664,7 b' class ConverterQuickHTML(Converter):' | |||||
639 |
|
664 | |||
640 | @DocInherit |
|
665 | @DocInherit | |
641 | def render_display_format_text(self, output): |
|
666 | def render_display_format_text(self, output): | |
642 |
return |
|
667 | return_list(output.text) | |
643 |
|
668 | |||
644 | @DocInherit |
|
669 | @DocInherit | |
645 | def _unknown_lines(self, data): |
|
670 | def _unknown_lines(self, data): | |
@@ -651,18 +676,14 b' class ConverterQuickHTML(Converter):' | |||||
651 |
|
676 | |||
652 | Returns list. |
|
677 | Returns list. | |
653 | """ |
|
678 | """ | |
654 |
|
|
679 | return_list(output.text) | |
655 | return output.text |
|
|||
656 | return [output.text] |
|
|||
657 |
|
680 | |||
658 | def render_display_format_html(self, output): |
|
681 | def render_display_format_html(self, output): | |
659 | """render the html part of an output |
|
682 | """render the html part of an output | |
660 |
|
683 | |||
661 | Returns list. |
|
684 | Returns list. | |
662 | """ |
|
685 | """ | |
663 |
|
|
686 | return_list(output.html) | |
664 | return output.html |
|
|||
665 | return [output.html] |
|
|||
666 |
|
687 | |||
667 | def render_display_format_latex(self, output): |
|
688 | def render_display_format_latex(self, output): | |
668 | """render the latex part of an output |
|
689 | """render the latex part of an output | |
@@ -686,9 +707,7 b' class ConverterQuickHTML(Converter):' | |||||
686 |
|
707 | |||
687 | Returns list. |
|
708 | Returns list. | |
688 | """ |
|
709 | """ | |
689 |
|
|
710 | return_list(output.javascript) | |
690 | return output.javascript |
|
|||
691 | return [output.javascript] |
|
|||
692 |
|
711 | |||
693 |
|
712 | |||
694 | class ConverterLaTeX(Converter): |
|
713 | class ConverterLaTeX(Converter): | |
@@ -1050,6 +1069,48 b' def rst2simplehtml(infile):' | |||||
1050 |
|
1069 | |||
1051 | return newfname |
|
1070 | return newfname | |
1052 |
|
1071 | |||
|
1072 | ||||
|
1073 | def md2html(infile): | |||
|
1074 | """Convert a markdown file to simplified html suitable for blogger. | |||
|
1075 | ||||
|
1076 | """ | |||
|
1077 | ||||
|
1078 | proc = subprocess.Popen(['markdown', infile], | |||
|
1079 | stdout=subprocess.PIPE, | |||
|
1080 | stderr=subprocess.PIPE) | |||
|
1081 | html, stderr = proc.communicate() | |||
|
1082 | if stderr: | |||
|
1083 | raise IOError(stderr) | |||
|
1084 | ||||
|
1085 | from pygments.formatters import HtmlFormatter | |||
|
1086 | css = HtmlFormatter().get_style_defs('.highlight') | |||
|
1087 | ||||
|
1088 | template = """ | |||
|
1089 | <!DOCTYPE HTML> | |||
|
1090 | <html> | |||
|
1091 | ||||
|
1092 | <head> | |||
|
1093 | <title>{infile}</title> | |||
|
1094 | ||||
|
1095 | <style type="text/css"> | |||
|
1096 | {css} | |||
|
1097 | </style> | |||
|
1098 | ||||
|
1099 | </head> | |||
|
1100 | ||||
|
1101 | <body> | |||
|
1102 | {html} | |||
|
1103 | </body> | |||
|
1104 | ||||
|
1105 | </html> | |||
|
1106 | """ | |||
|
1107 | full_html = template.format(**locals()) | |||
|
1108 | newfname = os.path.splitext(infile)[0] + '.html' | |||
|
1109 | with open(newfname, 'w') as f: | |||
|
1110 | f.write(full_html) | |||
|
1111 | ||||
|
1112 | return newfname | |||
|
1113 | ||||
1053 | #----------------------------------------------------------------------------- |
|
1114 | #----------------------------------------------------------------------------- | |
1054 | # Cell-level functions -- similar to IPython.nbformat.v3.rwbase functions |
|
1115 | # Cell-level functions -- similar to IPython.nbformat.v3.rwbase functions | |
1055 | # but at cell level instead of whole notebook level |
|
1116 | # but at cell level instead of whole notebook level | |
@@ -1064,7 +1125,10 b' def writes_cell(cell, **kwargs):' | |||||
1064 | cell = split_lines_cell(copy.deepcopy(cell)) |
|
1125 | cell = split_lines_cell(copy.deepcopy(cell)) | |
1065 | return py3compat.str_to_unicode(json.dumps(cell, **kwargs), 'utf-8') |
|
1126 | return py3compat.str_to_unicode(json.dumps(cell, **kwargs), 'utf-8') | |
1066 |
|
1127 | |||
|
1128 | ||||
1067 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
1129 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |
|
1130 | ||||
|
1131 | ||||
1068 | def split_lines_cell(cell): |
|
1132 | def split_lines_cell(cell): | |
1069 | """ |
|
1133 | """ | |
1070 | Split lines within a cell as in |
|
1134 | Split lines within a cell as in | |
@@ -1086,6 +1150,7 b' def split_lines_cell(cell):' | |||||
1086 | cell[key] = (item + '\n').splitlines() |
|
1150 | cell[key] = (item + '\n').splitlines() | |
1087 | return cell |
|
1151 | return cell | |
1088 |
|
1152 | |||
|
1153 | ||||
1089 | def cell_to_lines(cell): |
|
1154 | def cell_to_lines(cell): | |
1090 | ''' |
|
1155 | ''' | |
1091 | Write a cell to json, returning the split lines. |
|
1156 | Write a cell to json, returning the split lines. | |
@@ -1109,10 +1174,10 b" def main(infile, format='rst'):" | |||||
1109 | converter = ConverterMarkdown(infile) |
|
1174 | converter = ConverterMarkdown(infile) | |
1110 | converter.render() |
|
1175 | converter.render() | |
1111 | elif format == 'html': |
|
1176 | elif format == 'html': | |
1112 |
#Currently, conversion to html is a 2 step process, nb-> |
|
1177 | #Currently, conversion to html is a 2 step process, nb->md->html | |
1113 |
converter = Converter |
|
1178 | converter = ConverterMarkdown(infile, True) | |
1114 |
|
|
1179 | mdfname = converter.render() | |
1115 |
|
|
1180 | md2html(mdfname) | |
1116 | elif format == 'quick-html': |
|
1181 | elif format == 'quick-html': | |
1117 | converter = ConverterQuickHTML(infile) |
|
1182 | converter = ConverterQuickHTML(infile) | |
1118 | rstfname = converter.render() |
|
1183 | rstfname = converter.render() |
General Comments 0
You need to be logged in to leave comments.
Login now