File: sgt

package info (click to toggle)
python-xml 0.4.19981014-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,124 kB
  • ctags: 3,099
  • sloc: ansic: 9,075; python: 8,150; xml: 7,940; makefile: 84; sh: 41
file content (131 lines) | stat: -rw-r--r-- 2,682 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''sgt: a SGML (linuxdoc dtd) to HTML proof-of-concept converter.

Just for testing the dom package. 
'''

import sys
from dom.esis_builder import EsisBuilder
from dom.writer import HtmlLineariser

from dom.html import *
from dom.transformer import *


class Linuxdoc2Html(Transformer):
	simple_map = {
		'itemize': 'ul',
		'enum': 'ol',
		'descrip': 'dl',
		'item': 'li',
		'bf': 'b',
		'it': 'i',
		'sl': 'i',
		'tt': 'code',
		'code': 'hr/pre',
		'tag': 'dt/b',
		#'cite': 'i([ID])',
		#'sect': 'h1(=heading),!heading',
	}

	complex_map = [
		'item [in itemize] : li($)',
		'url : a(name=#url, #name)',
		'sect : h1([$.GI!=heading]),[$.GI=heading]',
	]

	def do_sect(self, node):
		return [
			H1(having(node.children, 'this.GI == "heading"')[0].children), 
		] +	having(node.children, 'this.GI != "heading"')

	def do_sect1(self, node):
		return [
			H2(having(node.children, 'this.GI == "heading"')[0].children), 
		] +	having(node.children, 'this.GI != "heading"')

	def do_sect2(self, node):
		return [
			H3(having(node.children, 'this.GI == "heading"')[0].children), 
		] +	having(node.children, 'this.GI != "heading"')

	def do_sect3(self, node):
		return [
			H3(having(node.children, 'this.GI == "heading"')[0].children), 
		] +	having(node.children, 'this.GI != "heading"')

	def do_sect4(self, node):
		return [
			H3(having(node.children, 'this.GI == "heading"')[0].children), 
		] +	having(node.children, 'this.GI != "heading"')


	def do_verb(self, node):
		return [PRE(node.children)]

	def do_tscreen(self, node):
		return node.children

	def do_titlepag(self, node):
		return []

	do_toc = do_titlepag

	def do_linuxdoc(self, node):
		return node.children

	def do_htmlurl(self, node):
		return [A({'href': node.attributes['URL']}, node.attributes['NAME'])]

	do_url = do_htmlurl

	def do_ref(self, node):
		return []

	def do_title(self, node):
		self.title = cdata(node.children)
		return []
		
	def do_abstract(self, node):
		self.abstract = cdata(node.children)
		return []


	def do_itemize(self, node):
		return [UL(node.attributes, node.children)]

	def do_enum(self, node):
		return [OL(node.attributes, node.children)]

	def do_item(self, node):
		return [LI(node.children)]
		
	def do_p(self, node):
		if len(node.children) > 0:
			return [node]
		else:
			return []

	def do_article(self, node):
		return [HTML(
			HEAD(
				TITLE(self.title),
				META(name='description', content=self.abstract),
			),
			BODY(node.children),
		)]

	def do_linuxdoc(self, node):
		return node.children


p = EsisBuilder()
p.feed(open(sys.argv[1]).read())

t = Linuxdoc2Html()
doc = t.transform(p.document)

w = HtmlLineariser()
print w.linearise(doc)


# vim:ts=2:ai