1
#! /usr/bin/env python
2
#
3
# Generate an HTML Snippet for BlogSpot from reStructuredText.
4
#
5
# Modified from Matthias Friedrich's script that
6
# Generate an HTML Snippet for WordPress Blogs from reStructuredText.
7
# http://unmaintainable.wordpress.com/2008/03/22/using-rst-with-wordpress/
8
#
9
# This is a modification of the standard HTML writer that leaves out
10
# the header, the body tag, and several CSS classes that have no use
11
# in wordpress. What is left is an incomplete HTML document suitable
12
# for pasting into the WordPress online editor.
13
#
14
# Note: This is a quick hack, so it probably won't work for the more
15
#       advanced features of rst.
16
#
17
# Copyright (c) 2008 Matthias Friedrich <matt@mafr.de>
18
# Copyright (c) 2009 Grissiom <chaos.proton@gmail.com>
19
#
20
# This program is free software; you can redistribute it and/or modify
21
# it under the terms of the Artistic License.
22
#
23
24
25
import sys
26
import docutils
27
import docutils.nodes
28
from docutils.writers import html4css1
29
from docutils import frontend, writers
30
from docutils.core import publish_cmdline, default_description
31
32
33
class Writer(html4css1.Writer):
34
	supported = ('bshtml', )
35
36
	settings_spec = html4css1.Writer.settings_spec + ( )
37
38
	def __init__(self):
39
		html4css1.Writer.__init__(self)
40
		self.translator_class = BsHtmlTranslator
41
42
43
class BsHtmlTranslator(html4css1.HTMLTranslator):
44
	"""An HTML emitting visitor."""
45
46
	doctype = ('')
47
48
	def __init__(self, *args):
49
		html4css1.HTMLTranslator.__init__(self, *args)
50
		self.stylesheet = [ ]
51
		self.meta = [ ]
52
		self.head = [ ]
53
		self.head_prefix = [ ]
54
		self.body_prefix = [ ]
55
		self.body_suffix = [ ]
56
		self.section_level = 3
57
		self.compact_simple = True
58
		self.literal_block = False
59
60
61
	def visit_document(self, node):
62
		pass
63
64
	def depart_document(self, node):
65
		pass
66
67
	def visit_section(self, node):
68
		self.section_level += 1
69
70
	def depart_section(self, node):
71
		self.section_level -= 1
72
73
	def visit_paragraph(self, node):
74
		if self.should_be_compact_paragraph(node):
75
			self.context.append('')
76
		else:
77
			self.body.append('<p>')
78
			self.context.append('</p>\n\n')
79
80
	def depart_paragraph(self, node):
81
		self.body.append(self.context.pop())
82
83
        #def visit_reference(self, node):
84
                #attrs = { }
85
                #if node.has_key('refuri'):
86
                        #attrs['href'] = node['refuri']
87
                #else:
88
                        #assert node.has_key('refid'), 'Invalid internal link'
89
                        #attrs['href'] = '#' + node['refid']
90
                #self.body.append(self.starttag(node, 'a', '', **attrs))
91
92
	def visit_Text(self, node):
93
		if self.literal_block:
94
			text = node.astext()
95
		else:
96
			text = node.astext().replace('\n', ' ')
97
		encoded = self.encode(text)
98
		if self.in_mailto and self.settings.cloak_email_addresses:
99
			encoded = self.cloak_email(encoded)
100
		self.body.append(encoded)
101
102
        def visit_block_quote(self, node):
103
                self.body.append('<blockquote>')
104
105
        def depart_block_quote(self, node):
106
                self.body.append('</blockquote>\n\n')
107
108
	#def visit_list_item(self, node):
109
		#self.body.append('  ' + self.starttag(node, 'li', ''))
110
111
	#def visit_title(self, node):
112
		#h_level = self.section_level + self.initial_header_level - 1
113
		#self.body.append(
114
			#self.starttag(node, 'h%s' % h_level, '', **{ }))
115
		#self.context.append('</h%s>\n\n' % (h_level, ))
116
117
	def depart_title(self, node):
118
		self.body.append(self.context.pop())
119
120
	def visit_literal_block(self, node):
121
		self.literal_block = True
122
		self.body.append(self.starttag(node, 'pre'))
123
124
	def depart_literal_block(self, node):
125
		self.body.append('\n</pre>\n\n')
126
		self.literal_block = False
127
128
	#def visit_literal(self, node):
129
		#self.body.append('<code>')
130
131
	#def depart_literal(self, node):
132
		#self.body.append('</code>')
133
134
135
if __name__ == '__main__':
136
	# docutils tries to load the module 'bshtml' below, so we need an alias
137
	sys.modules['bshtml'] = sys.modules['__main__']
138
139
	try:
140
	    import locale
141
	    locale.setlocale(locale.LC_ALL, '')
142
	except:
143
	    pass
144
145
	description = ('Generates an HTML Snippet for Wordpress from'
146
			'standalone reStructuredText sources.  '
147
			+ default_description)
148
149
	publish_cmdline(writer_name='bshtml', description=description)
150
151
# EOF