Commit 65fbba81 authored by Andrew Xia's avatar Andrew Xia

export function

parent 1ecb6f89
...@@ -15,9 +15,7 @@ FILE = "Week 10 37 to 313.html" ...@@ -15,9 +15,7 @@ FILE = "Week 10 37 to 313.html"
f = open(FOLDER + "/" + FILE) f = open(FOLDER + "/" + FILE)
for line in f: for line in f:
# print line, #print the files
CONTENT += line CONTENT += line
# print "load_content: done"
# CONTENT = """ # CONTENT = """
# <p>For <em>Implementing</em>, working with <a href="http://chiraag.scripts.mit.edu/wiki/start" target="_blank"><strong>Chiraag Juvekar</strong></a> and <a href="http://www-mtl.mit.edu/~anantha/" target="_blank"><strong>Prof. Anantha Chandrakasan</strong></a>.</p> # <p>For <em>Implementing</em>, working with <a href="http://chiraag.scripts.mit.edu/wiki/start" target="_blank"><strong>Chiraag Juvekar</strong></a> and <a href="http://www-mtl.mit.edu/~anantha/" target="_blank"><strong>Prof. Anantha Chandrakasan</strong></a>.</p>
...@@ -58,7 +56,5 @@ for line in f: ...@@ -58,7 +56,5 @@ for line in f:
# """ # """
converter = tomd.Tomd(CONTENT,FOLDER,FILE) converter = tomd.Tomd(CONTENT,FOLDER,FILE)
# print(converter.markdown())
# print(repr(tomd.convert(CONTENT)))
print(converter.convert())
\ No newline at end of file
...@@ -97,8 +97,8 @@ class Element: ...@@ -97,8 +97,8 @@ class Element:
if self.is_block: if self.is_block:
# print "parsing tag:", self.tag, ", content: ", repr(self.content) # print "parsing tag:", self.tag, ", content: ", repr(self.content)
self.parse_inline() self.parse_inline()
if self.tag != 'table': # if self.tag != 'table':
print "parsed:", self.tag, self.folder, ", content: ", repr(self.content) # print "parsed:", self.tag, self.folder, ", content: ", repr(self.content)
def __str__(self): def __str__(self):
wrapper = MARKDOWN.get(self.tag) wrapper = MARKDOWN.get(self.tag)
...@@ -126,7 +126,7 @@ class Element: ...@@ -126,7 +126,7 @@ class Element:
# if self.content != re.sub(BlOCK_ELEMENTS['table'], '\g<1>', self.content): # if self.content != re.sub(BlOCK_ELEMENTS['table'], '\g<1>', self.content):
for m in re.finditer(BlOCK_ELEMENTS['table'], self.content, re.I | re.S | re.M): for m in re.finditer(BlOCK_ELEMENTS['table'], self.content, re.I | re.S | re.M):
#hmm can there only be one table? #hmm can there only be one table?
print "AHHHH THERES A TABLE\n\n" # print "AHHHH THERES A TABLE\n\n"
inner = Element(start_pos=m.start(), inner = Element(start_pos=m.start(),
end_pos=m.end(), end_pos=m.end(),
content=''.join(m.groups()), content=''.join(m.groups()),
...@@ -199,7 +199,7 @@ class Tomd: ...@@ -199,7 +199,7 @@ class Tomd:
self.folder = folder self.folder = folder
self.file = file self.file = file
self.options = options # haven't been implemented yet self.options = options # haven't been implemented yet
self._markdown = '' self._markdown = self.convert(self.html,self.options)
def convert(self, html="", options=None): def convert(self, html="", options=None):
if html == "": if html == "":
...@@ -211,7 +211,7 @@ class Tomd: ...@@ -211,7 +211,7 @@ class Tomd:
for m in re.finditer(pattern, html, re.I | re.S | re.M): for m in re.finditer(pattern, html, re.I | re.S | re.M):
# now m contains the pattern without the tag # now m contains the pattern without the tag
# if tag == "e_p": # if tag == "e_p":
print "found", tag, m.groups(), "start", m.start(), "end", m.end(), self.folder # print "found", tag, m.groups(), "start", m.start(), "end", m.end(), self.folder
element = Element(start_pos=m.start(), element = Element(start_pos=m.start(),
end_pos=m.end(), end_pos=m.end(),
content=''.join(m.groups()), content=''.join(m.groups()),
...@@ -226,10 +226,10 @@ class Tomd: ...@@ -226,10 +226,10 @@ class Tomd:
elements.remove(e) elements.remove(e)
if can_append: if can_append:
elements.append(element) elements.append(element)
print "\n\n\ndone with convert, element is" # print "\n\n\ndone with convert, element is"
for e in elements: # for e in elements:
print repr(str(e)) # print repr(str(e))
print "---" # print "---"
elements.sort(key=lambda element: element.start_pos) elements.sort(key=lambda element: element.start_pos)
self._markdown = ''.join([str(e) for e in elements]) self._markdown = ''.join([str(e) for e in elements])
...@@ -242,6 +242,22 @@ class Tomd: ...@@ -242,6 +242,22 @@ class Tomd:
self.convert(self.html, self.options) self.convert(self.html, self.options)
return self._markdown return self._markdown
def export(self,folder=False):
if len(self.file) < 1:
warnings.warn("file not specified, renamed to tmp.md")
file = "tmp.md"
else:
file = self.file.replace('.html','.md') #rename to md
if len(self.folder) < 2:
warnings.warn("folder not specified, will save to pwd")
elif not folder:
file = self.folder + '/' + file
else: #if folder is specified
file = folder + '/' + file
f = open(file,'w')
f.write(self._markdown)
f.close()
_inst = Tomd() _inst = Tomd()
convert = _inst.convert convert = _inst.convert
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment