-
Notifications
You must be signed in to change notification settings - Fork 4
/
html2django.py
61 lines (46 loc) · 1.5 KB
/
html2django.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
#!/usr/bin/env python3
# This file converts between our html format and django template files.
# Pass the file to convert on the command line.
# Writes to stdout.
import sys
try:
from typing import TextIO, List # noqa F401
except ImportError:
print("WARNING!")
if len(sys.argv) < 2:
print("Must supply an HTML file.")
exit(1)
html_file = sys.argv[1] # type: str
f = open(html_file, "r") # type: TextIO
input = f.readlines() # type: List[str]
f.close()
# - we need to go until we start seeing text that wil appear on screen
# - These tags could include: <hn> - where n can be 1 - 5
# or <p> or <figure> or <img> or <div>
pos1 = 0 # type: int
for i in range(len(input)):
if "<!DOCTYPE html>" in input[i]:
pos1 = i
pos2 = 0 # type: int
for i in range(len(input)):
if "<h1" in input[i] or "<p" in input[i] or "<h2" in input[i] \
or "<h3" in input[i] or "<h4" in input[i] or "<h5" in input[i]\
or "<figure" in input[i] or "<div" in input[i]:
pos2 = i
break
del input[pos1:pos2]
pos3 = 0 # type: int
for line in range(len(input)):
if "</body>" in input[line]:
pos3 = line
del input[pos3:]
output = [] # type: List[str]
output.append("""{% extends "base.html" %}""""\n")
output.append("""{% block content %}""""\n")
output.append("""<div class="module">""""\n")
for i in range(len(input)):
output.append(input[i])
output.append("""</div>""""\n")
output.append("""{% endblock content %}""")
for line in output:
print(line, end="")