aboutsummaryrefslogtreecommitdiffstats
path: root/src/renderer/html.rs
blob: b3ee3432d4a86b7f4f9ea89e5035d50f8669891a (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use std::io::Write;

use failure::Error;

// use crate::url::Url;
use crate::document_tree::{
	Document,
	HasChildren,
	ExtraAttributes,
	elements as e,
	element_categories as c,
	extra_attributes as a,
};


// static FOOTNOTE_SYMBOLS: [char; 10] = ['*', '†', '‡', '§', '¶', '#', '♠', '♥', '♦', '♣'];

pub fn render_html<W>(document: &Document, mut stream: W, standalone: bool) -> Result<(), Error> where W: Write {
	if standalone {
		document.render_html(stream.by_ref())
	} else {
		let stream = stream.by_ref();
		for c in document.children() {
			(*c).render_html(stream)?;
		}
		Ok(())
	}
}

fn escape_html(text: &str) -> String {
	text.replace('&', "&amp;")
		.replace('<', "&lt;")
		.replace('>', "&gt;")
		.replace('"', "&quot;")
}

trait HTMLRender {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write;
}

macro_rules! impl_html_render_cat {($cat:ident { $($member:ident),+ }) => {
	impl HTMLRender for c::$cat {
		fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
			match self {$(
				c::$cat::$member(elem) => (**elem).render_html(stream),
			)+}
		}
	}
}}

macro_rules! impl_html_render_simple {( $($type:ident => $tag:ident),+ ) => { $(
	impl HTMLRender for e::$type {
		fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
			write!(stream, "<{}>", stringify!($tag))?;
			for c in self.children() {
				(*c).render_html(stream)?;
			}
			write!(stream, "</{}>", stringify!($tag))?;
			Ok(())
		}
	}
)+ }}

macro_rules! impl_html_render_simple_nochildren {( $($type:ident => $tag:ident),+ ) => { $(
	impl HTMLRender for e::$type {
		fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
			write!(stream, "<{0}></{0}>", stringify!($tag))?;
			Ok(())
		}
	}
)+ }}

// Impl

impl HTMLRender for Document {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		write!(stream, "<!doctype html><html>")?;
		for c in self.children() {
			(*c).render_html(stream)?;
		}
		write!(stream, "</html>")?;
		Ok(())
	}
}

impl_html_render_cat!(StructuralSubElement { Title, Subtitle, Decoration, Docinfo, SubStructure });
impl_html_render_simple!(Title => h1, Subtitle => h2);

impl HTMLRender for e::Docinfo {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Like “YAML frontmatter” in Markdown
		unimplemented!();
	}
}

impl HTMLRender for e::Decoration {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Header or footer
		unimplemented!();
	}
}

impl_html_render_cat!(SubStructure { Topic, Sidebar, Transition, Section, BodyElement });
impl_html_render_simple!(Sidebar => aside, Section => section);

impl HTMLRender for e::Transition {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		write!(stream, "<hr/>")?;
		Ok(())
	}
}

impl HTMLRender for e::Topic {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// A mini section with title
		unimplemented!();
	}
}

impl_html_render_cat!(BodyElement { Paragraph, LiteralBlock, DoctestBlock, MathBlock, Rubric, SubstitutionDefinition, Comment, Pending, Target, Raw, Image, Compound, Container, BulletList, EnumeratedList, DefinitionList, FieldList, OptionList, LineBlock, BlockQuote, Admonition, Attention, Hint, Note, Caution, Danger, Error, Important, Tip, Warning, Footnote, Citation, SystemMessage, Figure, Table });
impl_html_render_simple!(Paragraph => p, LiteralBlock => pre, MathBlock => math, Rubric => a, Compound => p, Container => div, BulletList => ul, EnumeratedList => ol, DefinitionList => dl, FieldList => dl, OptionList => pre, LineBlock => div, BlockQuote => blockquote, Admonition => aside, Attention => aside, Hint => aside, Note => aside, Caution => aside, Danger => aside, Error => aside, Important => aside, Tip => aside, Warning => aside, Figure => figure);
impl_html_render_simple_nochildren!(Table => table);  //TODO: after implementing the table, move it to elems with children

impl<I> HTMLRender for I where I: e::Element + a::ExtraAttributes<a::Image> {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		let extra = self.extra();
		if let Some(ref target) = extra.target {
			write!(stream, "<a href=\"{}\">", escape_html(target.as_str()))?;
		}
		write!(stream, "<img")?;
		if let Some(ref alt) = extra.alt {
			write!(stream, " alt=\"{}\"", escape_html(alt))?;
		}
		// TODO: align: Option<AlignHV>
		// TODO: height: Option<Measure>
		// TODO: width: Option<Measure>
		// TODO: scale: Option<u8>
		write!(stream, " src=\"{}\" />", escape_html(extra.uri.as_str()))?;
		if extra.target.is_some() {
			write!(stream, "</a>")?;
		}
		Ok(())
	}
}

impl HTMLRender for e::DoctestBlock {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// TODO
		unimplemented!();
	}
}

impl HTMLRender for e::SubstitutionDefinition {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// TODO: Should those be removed after resolving them
		Ok(())
	}
}

impl HTMLRender for e::Comment {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		write!(stream, "<!--")?;
		for c in self.children() {
			(*c).render_html(stream)?;
		}
		write!(stream, "-->")?;
		Ok(())
	}
}

impl HTMLRender for e::Pending {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Will those be resolved by the time we get here?
		unimplemented!();
	}
}

impl HTMLRender for e::Target {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Should be resolved by now
		Ok(())
	}
}

impl HTMLRender for e::Raw {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		for c in self.children() {
			write!(stream, "{}", c)?;
		}
		Ok(())
	}
}

impl HTMLRender for e::Footnote {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		unimplemented!();
	}
}

impl HTMLRender for e::Citation {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		unimplemented!();
	}
}

impl HTMLRender for e::SystemMessage {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		write!(stream, "<figure><caption>System Message</caption>")?;
		for c in self.children() {
			(*c).render_html(stream)?;
		}
		write!(stream, "</figure>")?;
		Ok(())
	}
}

impl_html_render_cat!(TextOrInlineElement { String, Emphasis, Strong, Literal, Reference, FootnoteReference, CitationReference, SubstitutionReference, TitleReference, Abbreviation, Acronym, Superscript, Subscript, Inline, Problematic, Generated, Math, TargetInline, RawInline, ImageInline });
impl_html_render_simple!(Emphasis => em, Strong => strong, Literal => code, FootnoteReference => a, CitationReference => a, TitleReference => a, Abbreviation => abbr, Acronym => acronym, Superscript => sup, Subscript => sub, Inline => span, Math => math, TargetInline => a);

impl HTMLRender for String {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		write!(stream, "{}", escape_html(self))?;
		Ok(())
	}
}

impl HTMLRender for e::Reference {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		let extra = self.extra();
		write!(stream, "<a")?;
		if let Some(ref target) = extra.refuri {
			write!(stream, " href=\"{}\"", escape_html(target.as_str()))?;
		}
		/*
		if let Some(ref name) = extra.name {
			write!(stream, " title=\"{}\"", escape_html(&name.0))?;
		}
		*/
		write!(stream, ">")?;
		for c in self.children() {
			(*c).render_html(stream)?;
		}
		write!(stream, "</a>")?;
		Ok(())
	}
}

impl HTMLRender for e::SubstitutionReference {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Will those be resolved by the time we get here?
		unimplemented!();
	}
}

impl HTMLRender for e::Problematic {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Broken inline markup leads to insertion of this in docutils
		unimplemented!();
	}
}

impl HTMLRender for e::Generated {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Section numbers and so on
		unimplemented!();
	}
}

impl HTMLRender for e::RawInline {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		for c in self.children() {
			write!(stream, "{}", c)?;
		}
		Ok(())
	}
}


//--------------\\
//Content Models\\
//--------------\\

impl_html_render_cat!(SubTopic { Title, BodyElement });
impl_html_render_cat!(SubSidebar { Topic, Title, Subtitle, BodyElement });
impl_html_render_simple!(ListItem => li);

impl HTMLRender for e::DefinitionListItem {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// Term→dt, Definition→dd, Classifier→???
		unimplemented!();
	}
}

impl HTMLRender for e::Field {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// FieldName→dt, FieldBody→dd
		unimplemented!();
	}
}

impl HTMLRender for e::OptionListItem {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		// OptionGroup→dt(s), Description→dd
		unimplemented!();
	}
}

impl_html_render_cat!(SubLineBlock { LineBlock, Line });

impl HTMLRender for e::Line {
	fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write {
		for c in self.children() {
			(*c).render_html(stream)?;
		}
		write!(stream, "<br>")?;
		Ok(())
	}
}

impl_html_render_cat!(SubBlockQuote { Attribution, BodyElement });
impl_html_render_simple!(Attribution => cite); //TODO: correct?

impl_html_render_cat!(SubFigure { Caption, Legend, BodyElement });
impl_html_render_simple!(Caption => caption);

impl HTMLRender for e::Legend {
	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write {
		unimplemented!();
	}
}

//------------\\
//Things to do\\
//------------\\

//TODO: prettyprint option list
//TODO: render admonitions: Admonition, Attention, Hint, Note, Caution, Danger, Error, Important, Tip, Warning
//TODO: properly render tables

//TODO: add reference target: FootnoteReference, CitationReference, TitleReference
//TODO: add title: Abbr, Acronym
//TODO: convert math, set display attr
//TODO: add id: Rubric, Target, TargetInline