add support for ipython/jupyter notebooks
This commit is contained in:
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (C) 2021 J.F.Dockes
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the
|
||||
# Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
# Recoll handler for iPython / Jupyter notebook files.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
import rclexecm
|
||||
from rclbasehandler import RclBaseHandler
|
||||
|
||||
class IPYNBextractor(RclBaseHandler):
|
||||
|
||||
def __init__(self, em):
|
||||
super(IPYNBextractor, self).__init__(em)
|
||||
|
||||
def html_text(self, fn):
|
||||
text = open(fn, 'rb').read()
|
||||
data = json.loads(text)
|
||||
mdtext = ""
|
||||
for cell in data["cells"]:
|
||||
if cell["cell_type"] == "markdown":
|
||||
mdtext += "\n"
|
||||
for line in cell["source"]:
|
||||
mdtext += "# " + line
|
||||
mdtext += "\n"
|
||||
elif cell["cell_type"] == "code":
|
||||
mdtext += "\n\n"
|
||||
for line in cell["source"]:
|
||||
mdtext += line
|
||||
mdtext += "\n"
|
||||
print("%s"%mdtext, file=sys.stderr)
|
||||
self.outputmimetype = 'text/plain'
|
||||
return mdtext
|
||||
|
||||
|
||||
# Main program: create protocol handler and extractor and run them
|
||||
proto = rclexecm.RclExecM()
|
||||
extract = IPYNBextractor(proto)
|
||||
rclexecm.main(proto, extract)
|
||||
Reference in New Issue
Block a user