##// END OF EJS Templates
Updates to markdown conversion.
Fernando Perez -
Show More
@@ -459,9 +459,26 b' class ConverterRST(Converter):'
459 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 475 class ConverterMarkdown(Converter):
463 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 482 @DocInherit
466 483 def render_heading(self, cell):
467 484 return ['{0} {1}'.format('#'*cell.level, cell.source), '']
@@ -473,7 +490,9 b' class ConverterMarkdown(Converter):'
473 490 lines = []
474 491 #lines.append('----')
475 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 496 if cell.outputs:
478 497 lines.extend(['==>', ''])
479 498 for output in cell.outputs:
@@ -487,7 +506,6 b' class ConverterMarkdown(Converter):'
487 506 @DocInherit
488 507 def render_markdown(self, cell):
489 508 return [cell.source, '']
490 #return [markdown2rst(cell.source)]
491 509
492 510 @DocInherit
493 511 def render_raw(self, cell):
@@ -499,14 +517,16 b' class ConverterMarkdown(Converter):'
499 517 @DocInherit
500 518 def render_pyout(self, output):
501 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 524 # output is a dictionary like object with type as a key
505 525 if 'latex' in output:
506 526 pass
507 527
508 528 if 'text' in output:
509 lines.extend([indent(output.text)])
529 lines.extend(['<pre>', indent(output.text), '</pre>'])
510 530
511 531 lines.append('')
512 532 return lines
@@ -558,6 +578,11 b' class ConverterMarkdown(Converter):'
558 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 586 class ConverterQuickHTML(Converter):
562 587 extension = 'html'
563 588
@@ -639,7 +664,7 b' class ConverterQuickHTML(Converter):'
639 664
640 665 @DocInherit
641 666 def render_display_format_text(self, output):
642 return [output.text]
667 return_list(output.text)
643 668
644 669 @DocInherit
645 670 def _unknown_lines(self, data):
@@ -651,18 +676,14 b' class ConverterQuickHTML(Converter):'
651 676
652 677 Returns list.
653 678 """
654 if type(output.text) == type([]):
655 return output.text
656 return [output.text]
679 return_list(output.text)
657 680
658 681 def render_display_format_html(self, output):
659 682 """render the html part of an output
660 683
661 684 Returns list.
662 685 """
663 if type(output.html) == type([]):
664 return output.html
665 return [output.html]
686 return_list(output.html)
666 687
667 688 def render_display_format_latex(self, output):
668 689 """render the latex part of an output
@@ -686,9 +707,7 b' class ConverterQuickHTML(Converter):'
686 707
687 708 Returns list.
688 709 """
689 if type(output.javascript) == type([]):
690 return output.javascript
691 return [output.javascript]
710 return_list(output.javascript)
692 711
693 712
694 713 class ConverterLaTeX(Converter):
@@ -1050,6 +1069,48 b' def rst2simplehtml(infile):'
1050 1069
1051 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 1115 # Cell-level functions -- similar to IPython.nbformat.v3.rwbase functions
1055 1116 # but at cell level instead of whole notebook level
@@ -1064,7 +1125,10 b' def writes_cell(cell, **kwargs):'
1064 1125 cell = split_lines_cell(copy.deepcopy(cell))
1065 1126 return py3compat.str_to_unicode(json.dumps(cell, **kwargs), 'utf-8')
1066 1127
1128
1067 1129 _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
1130
1131
1068 1132 def split_lines_cell(cell):
1069 1133 """
1070 1134 Split lines within a cell as in
@@ -1086,6 +1150,7 b' def split_lines_cell(cell):'
1086 1150 cell[key] = (item + '\n').splitlines()
1087 1151 return cell
1088 1152
1153
1089 1154 def cell_to_lines(cell):
1090 1155 '''
1091 1156 Write a cell to json, returning the split lines.
@@ -1109,10 +1174,10 b" def main(infile, format='rst'):"
1109 1174 converter = ConverterMarkdown(infile)
1110 1175 converter.render()
1111 1176 elif format == 'html':
1112 #Currently, conversion to html is a 2 step process, nb->rst->html
1113 converter = ConverterRST(infile)
1114 rstfname = converter.render()
1115 rst2simplehtml(rstfname)
1177 #Currently, conversion to html is a 2 step process, nb->md->html
1178 converter = ConverterMarkdown(infile, True)
1179 mdfname = converter.render()
1180 md2html(mdfname)
1116 1181 elif format == 'quick-html':
1117 1182 converter = ConverterQuickHTML(infile)
1118 1183 rstfname = converter.render()
General Comments 0
You need to be logged in to leave comments. Login now