Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ all:
@echo 'please run "make publish" to publish'

publish:
python2 setup.py sdist bdist_wheel
python3 setup.py sdist bdist_wheel
twine upload dist/*

clean:
Expand Down
23 changes: 14 additions & 9 deletions bin/pdf2md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#!/usr/bin/env python2
#!/usr/bin/env python3

import sys
import os

PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

import pdf2md

def main(argv):
if len(argv) == 2:
filename = argv[1]
title = os.path.splitext(os.path.basename(filename))[0]
print 'Parsing', filename
else:
print 'usage:'
print ' python main.py <pdf>'
if len(argv) < 2 or argv[1] in ['-h', '--help']:
print('usage:')
print(' {} <pdf>'.format(argv[0]))
return

filename = argv[1]
title = os.path.splitext(os.path.basename(filename))[0]
print('Parsing', filename)

parser = pdf2md.Parser(filename)
parser.extract()
piles = parser.parse()
Expand All @@ -26,7 +31,7 @@ def main(argv):
writer.set_title(title)
writer.write(piles)

print 'Your markdown is at', writer.get_location()
print('Your markdown is at', writer.get_location())

if __name__ == '__main__':
main(sys.argv)
Expand Down
8 changes: 4 additions & 4 deletions pdf2md/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parser import Parser
from writer import Writer
from syntax import BaseSyntax
from syntax import UrbanSyntax
from .parser import Parser
from .writer import Writer
from .syntax import BaseSyntax
from .syntax import UrbanSyntax
2 changes: 1 addition & 1 deletion pdf2md/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pdfminer.pdfdevice import PDFDevice
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
from pile import Pile
from .pile import Pile


class Parser(object):
Expand Down
20 changes: 12 additions & 8 deletions pdf2md/pile.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse_layout(self, layout):
elif type(obj) == LTChar:
pass
elif type(obj) == LTLine:
pass
pass
else:
assert False, "Unrecognized type: %s" % type(obj)

Expand All @@ -63,7 +63,9 @@ def split_piles(self):
paragraphs = self._find_paragraphs(tables)
images = self._find_images()

piles = sorted(tables + paragraphs + images, reverse=True, key=lambda x: x._get_anything().y0)
piles = tables + paragraphs + images
piles = list(filter(lambda x: x._empty(), piles))
piles = sorted(piles, reverse=True, key=lambda x: x._get_anything().y0)

return piles

Expand Down Expand Up @@ -99,7 +101,7 @@ def gen_html(self):
'height': text.y1 - text.y0,
'x': text.x0,
'y': text.y0,
'text': text.get_text().encode('utf8'),
'text': text.get_text(),
'fill': 'green',
}
html += rect.format(**info)
Expand Down Expand Up @@ -202,7 +204,7 @@ def _find_paragraphs(self, tables):
paragraphs[idx].texts.append(text)
break

paragraphs = filter(None, paragraphs)
paragraphs = list(filter(lambda x: x._empty(), paragraphs))

return paragraphs

Expand All @@ -225,7 +227,7 @@ def _get_anything(self):


def _is_overlap(self, top, bottom, obj):
assert top > bottom
assert top >= bottom
return (bottom - self._SEARCH_DISTANCE) <= obj.y0 <= (top + self._SEARCH_DISTANCE) or \
(bottom - self._SEARCH_DISTANCE) <= obj.y1 <= (top + self._SEARCH_DISTANCE)

Expand Down Expand Up @@ -350,9 +352,9 @@ def _is_ignore_cell(self, left, top, right, bottom):


def _find_exist_coor(self, minimum, maximum, start_idx, line_coor, direction):
span = 0
span = -1
line_exist = False
while not line_exist:
while not line_exist and start_idx+span+1 < len(line_coor):
span += 1
coor = line_coor[start_idx + span]
line_exist = self._line_exists(coor, minimum, maximum, direction)
Expand Down Expand Up @@ -411,7 +413,7 @@ def _create_tag(self, tag_name, start, level):

def _create_td_tag(self, cell):
indent = '\t' * 2
texts = [text.get_text().encode('utf8').strip() for text in cell['texts']]
texts = [text.get_text().strip() for text in cell['texts']]
texts = ' '.join(texts)
colspan = ' colspan={}'.format(cell['colspan']) if 'colspan' in cell else ''
rowspan = ' rowspan={}'.format(cell['rowspan']) if 'rowspan' in cell else ''
Expand All @@ -426,4 +428,6 @@ def _calc_coordinates(self, axes, attr, reverse):
coor_list.sort(reverse=reverse)
return coor_list

def _empty(self):
return bool(self.images) or bool(self.texts)

8 changes: 4 additions & 4 deletions pdf2md/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def newline(self, text):


def purify(self, text):
return text.get_text().encode('utf8').strip()
return text.get_text().strip()


class UrbanSyntax(BaseSyntax):
Expand All @@ -27,7 +27,7 @@ def __init__(self):


def pattern(self, text):
content = text.get_text().encode('utf8').strip()
content = text.get_text().strip()

if not content:
return 'none'
Expand Down Expand Up @@ -62,7 +62,7 @@ def pattern(self, text):
return 'plain-text'

def newline(self, text):
content = text.get_text().encode('utf8').strip()
content = text.get_text().strip()

if text.x0 < 90.1: # special case for neihu page 2
return True
Expand All @@ -78,7 +78,7 @@ def newline(self, text):


def purify(self, text):
content = text.get_text().encode('utf8').strip()
content = text.get_text().strip()

mo = re.match('(一|二|三|四|五|六|七|八|九|十)、(.*)', content)
if mo:
Expand Down