- custom formatters done

- json formatter works now
- added some tests for writers
This commit is contained in:
ari committed 2018-12-22 22:44:53 -05:00
1 parent 3ba0b95001
commit bc20627d9f
21 files changed
+981 -552

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
from os import remove
from tempfile import mktemp
from pdfmajor.converters.writers.html import HTMLMaker
# Make temp file
FILE_NAME = mktemp()
with HTMLMaker(FILE_NAME) as html:
with html.elm("html", nolineend=True):
html.write("test")
assert(open(FILE_NAME, 'r').read() == """<html >
test
</html>""")
with HTMLMaker(FILE_NAME) as html:
with html.elm("html", nolineend=True):
with html.elm("head"):
html.singleton("meta", {
'http-equiv': "Content-Type",
'content': "text/html"
})
assert(open(FILE_NAME, 'r').read() == """<html >
<head >
<meta http-equiv="Content-Type" content="text/html" />
</head>
</html>""")
with HTMLMaker(FILE_NAME) as html:
with html.elm("html", nolineend=True):
with html.elm("head"):
html.singleton("meta", {
'http-equiv': "Content-Type",
'content': "text/html"
})
with html.elm("body"):
with html.elm("h1", css={"color": "red"}):
html.write("Hello World")
assert(open(FILE_NAME, 'r').read() == """<html >
<head >
<meta http-equiv="Content-Type" content="text/html" />
</head>
<body >
<h1 style="color: red">
Hello World
</h1>
</body>
</html>""")
with HTMLMaker(FILE_NAME) as html:
with html.elm('style'):
html.write('''.page { position: relative; overflow: hidden;border: 1px black solid;}''')
assert(open(FILE_NAME, 'r').read() == """<style >
.page { position: relative; overflow: hidden;border: 1px black solid;}
</style>
""")
# Clean up
remove(FILE_NAME)
+1 -1
View File
@@ -9,7 +9,7 @@ from pdfmajor.interpreter.commands import LTCharBlock, LTChar
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
FILE_NAME = os.path.join(
"../" if CUR_PATH == "" else CUR_PATH,
"samples/pdf/tables.pdf"
"samples/pdf/long-tables.pdf"
)
for page in PDFInterpreter(FILE_NAME, debug_level=logging.INFO):
+112
View File
@@ -0,0 +1,112 @@
from os import remove
from tempfile import mktemp
from pdfmajor.converters.writers.json import JSONMaker
# Make temp file
FILE_NAME = mktemp()
with JSONMaker(FILE_NAME) as obj:
obj.number("a", 2)
obj.string("hello", "world")
assert(open(FILE_NAME, 'r').read() == """{
"a": 2,
"hello": "world"
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.object("test") as child:
child.number("A", 2)
assert(open(FILE_NAME, 'r').read() == """{
"test": {
"A": 2
}
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.object("hello") as child:
with child.object("nested") as nested:
with nested.object("so") as so:
so.string("really", 'nested'),
so.number("man", 2)
nested.number("he", 3)
child.string('fo', 'real')
obj.string('fo', 'real2')
assert(open(FILE_NAME, 'r').read() == """{
"hello": {
"nested": {
"so": {
"really": "nested",
"man": 2
},
"he": 3
},
"fo": "real"
},
"fo": "real2"
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.array("hello") as child:
child.number(1)
with child.array() as nchild:
nchild.string('wow')
child.number(3)
assert(open(FILE_NAME, 'r').read() == """{
"hello": [
1,
[
"wow"
],
3
]
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.array("pages") as pages:
with pages.object() as page:
page.number("id", 1)
page.number("height", 20)
page.number("width", 20)
with page.array("children") as children:
with children.object() as item:
item.string("type", "char-block")
item.string("text", "test")
item.string("color_type", "rgb")
with item.array("color_vals") as col_vals:
col_vals.number(0)
col_vals.number(0)
col_vals.number(0)
item.number("x0", 2)
item.number("x1", 4.2)
item.number("y0", 231.12)
item.number("y1", 331.12)
print(open(FILE_NAME, 'r').read())
assert(open(FILE_NAME, 'r').read() == """{
"pages": [
{
"id": 1,
"height": 20,
"width": 20,
"children": [
{
"type": "char-block",
"text": "test",
"color_type": "rgb",
"color_vals": [
0,
0,
0
],
"x0": 2,
"x1": 4.2,
"y0": 231.12,
"y1": 331.12
}
]
}
]
}""")
# Clean up
remove(FILE_NAME)