Created
February 23, 2017 08:08
-
-
Save inakagawa/11fe11d84181bda634841c1534b82381 to your computer and use it in GitHub Desktop.
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
# markdown headline | |
import sys | |
import re | |
import pprint | |
''' | |
ベタ/tab-outline/markdownの変換 | |
元が何か判別 | |
変換先は何か | |
コマンドラインオプション | |
''' | |
def main(): | |
fh = get_fh() | |
readdata = fh.readlines() | |
# データをパース、解釈 | |
inner_data = arrange_data(data) | |
# outdata = arrange_data(data) | |
for line in outdata: | |
print(line) | |
def get_fh(): | |
fh = None | |
if (len(sys.argv)>1): | |
# ファイルの存在確認、なければ終了 | |
datafile = sys.argv[1] | |
fh = open(datafile,mode="r",encoding="utf-8") | |
else: | |
fh = sys.stdin | |
return fh | |
def read_from_fh(fh): | |
data = [] | |
with fh: | |
ln = fh.readlines() | |
for l in ln: | |
# l=l.rstrip() | |
l=l.strip() | |
if re.search('^#{2,}',l): | |
data.append(l) | |
else: | |
continue | |
return data | |
def set_data_structure(data): | |
# remove null line | |
# (group lines) | |
# determine type | |
# check '-': markdown | |
# check spaces: outline | |
# none: plaintext | |
pass | |
class CnvMarkdown: | |
NAME='md' | |
def __init__(self): | |
pass | |
def check(self,line): | |
re.search('^\-',line) | |
def strip_all(self,lines): | |
ra = [] | |
for line in lines: | |
line = re.sub('^\-','',line) | |
ra.append({'lv':0, 'text':line}) | |
return ra | |
def add_all(self,data_lines): | |
ra = [] | |
for dl in data_lines: | |
ra.append('- ' + dl['text']) | |
return ra | |
class CnvTabtext: | |
NAME='tab' | |
def __init__(self): | |
pass | |
def check(self,line): | |
re.search('^\s+',line) | |
def strip_all(self,lines): | |
ra = [] | |
for line in lines: | |
line = re.sub('^\s+','',line) | |
ra.append({'lv':0, 'text':line}) | |
return ra | |
def add_all(self,data_lines): | |
ra = [] | |
for dl in data_lines: | |
ra.append('- ' + dl['text']) | |
return ra | |
lines = 'aaa bbb ccc ddd'.split() | |
cM = CnvMarkdown() | |
i_lines = cM.strip_all(lines) | |
o_lines = cM.add_all(i_lines) | |
print(cM.NAME) | |
print(o_lines) | |
## debug output | |
# pprint.PrettyPrinter(indent=2).pprint('test') | |
#if __name__ == "__main__": | |
# main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment