-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyml_test.py
78 lines (70 loc) · 1.93 KB
/
yml_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
##########################################################
# External dependencies (pip install required):
# * markdown
# * docutils
#
# Python installed dependencies:
# * yaml
# * urllib
# * BeautifulSoup
#
# Input is YAML file
#
# Code inspiration for rst conversion:
# https://stackoverflow.com/questions/32167384/how-do-i-convert-a-docutils-document-tree-into-an-html-string
#
# Kevin Putnam
#
#
##########################################################
import yaml
import markdown
from docutils.core import publish_doctree, publish_from_doctree
import urllib
from bs4 import BeautifulSoup
with open("testing.yml", 'r') as stream:
try:
yamlObject = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
html = """
<html>
<head>
<style>
body {
font-family: arial, sans-serif;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #000000;
text-align: center;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>"""
if 'md' in yamlObject['additional_content']:
with open(yamlObject['additional_content']['md']) as mdF:
mdContent = mdF.read()
html += markdown.markdown(mdContent)
if 'rst' in yamlObject['additional_content']:
with open(yamlObject['additional_content']['rst']) as rstF:
rstContent = rstF.read()
tree = publish_doctree(rstContent)
htmlOutput = publish_from_doctree(tree, writer_name='html').decode()
soup = BeautifulSoup(htmlOutput, features='lxml')
body = soup.find('body')
htmlOutput = body.findChildren()
html += str(htmlOutput[0])
html += """ </body>
</html>"""
with open("testing.html", 'w') as f:
f.write(html)