-
-
Save sydney0zq/4a23ef9caa00f6efa29f1c68815ca7da to your computer and use it in GitHub Desktop.
Evernote Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# vim:fenc=utf-8 | |
# | |
# Copyright © 2018 zhou <zhou@Macbook> | |
# | |
# Distributed under terms of the MIT license. | |
""" | |
Export txt document to desired format in HTML. | |
""" | |
import os | |
import re | |
import sys | |
class exporter: | |
def __init__(self, txtfn, dumpto=None): | |
self.txtfn = txtfn | |
self.dumpto = dumpto | |
self.htmlframe = """<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Exporter</title> | |
</head> | |
<body> | |
<pre>{} | |
</pre> | |
</body> | |
</html>""" | |
self.checkvalid() | |
self.export() | |
self.dump() | |
def checkvalid(self): | |
assert (os.path.isfile(self.txtfn)), "{} not exists".format(self.txtfn) | |
with open(self.txtfn) as f: | |
content = f.read() | |
assert (content.count('\*\*') % 2 == 0), "Content's ** number({}) is not legal".format(content.count('\*\*')) | |
self.content = content | |
print ("CHECK VALIDTION OVER...") | |
def export(self): | |
content = self.content.replace('\t', ' '*4) | |
pattern = re.compile('\*\*') | |
piter = pattern.finditer(content) | |
content_ = content | |
for i, match in enumerate(piter): | |
l, r = match.span()[0], match.span()[1] | |
if i % 2 == 0: | |
l += (i // 2) * len("<b></b>") | |
content_ = content_[:l] + "<b>" + content_[l:] | |
else: | |
r += (i // 2) * len("<b></b>") + len("<b>") | |
content_ = content_[:r] + "</b>"+ content_[r:] | |
self.content = self.htmlframe.format(content_) | |
def dump(self): | |
if self.dumpto == None: | |
print(self.content) | |
return | |
assert (type(self.dumpto) == type('')), "Dump direction should be stdout or filename..." | |
if not os.path.exists(self.dumpto): | |
with open(self.dumpto, "w") as f: | |
f.write(self.content) | |
else: | |
print ("NOT WRITTEN -- {} EXISTS...".format(self.dumpto)) | |
if __name__ == "__main__": | |
if len(sys.argv) == 2: | |
exporter(sys.argv[1]) | |
elif len(sys.argv) == 3: | |
exporter(sys.argv[1], sys.argv[2]) | |
else: | |
print ('Usage:\n\t{} INPUT_FN [OUT_FN]'.format(sys.argv[0])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment