forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
markdown.cpp.html
134 lines (129 loc) · 20.5 KB
/
markdown.cpp.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>markdown.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">/* A more modern markdown converter.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * Copyright (c) 2014 by Aaron Bloomfield</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * This is part of the </span></i><u><span style="color:#0000FF">https://github.com/aaronbloomfield/pdr</span></u>
<i><span style="color:#9A1900"> * repository, and is released under the CC BY-SA license (along with</span></i>
<i><span style="color:#9A1900"> * the rest of that repository).</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * The default markdown that is installed with Ubuntu (version 1.0.1</span></i>
<i><span style="color:#9A1900"> * from 2004) does not support more modern tags, such as tables. This</span></i>
<i><span style="color:#9A1900"> * program will use the libmarkdown library, which does have support</span></i>
<i><span style="color:#9A1900"> * for those tags, to perform a markdown conversion. The full list of</span></i>
<i><span style="color:#9A1900"> * additional features it supports can be found at</span></i>
<i><span style="color:#9A1900"> * </span></i><u><span style="color:#0000FF">http://manpages.ubuntu.com/manpages/raring/man7/mkd-extensions.7.html.</span></u>
<i><span style="color:#9A1900"> * It requires the libmarkdown2-dev package be installed under</span></i>
<i><span style="color:#9A1900"> * Ubuntu.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * The resulting document is a full HTML document -- meaning it adds</span></i>
<i><span style="color:#9A1900"> * the <doctype>, <html>, <head>, and <body> tags to the document.</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * To compile: "g++ markdown.cpp -o markdown -lmarkdown"</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * To use, specify the input file name, and optionally an output file</span></i>
<i><span style="color:#9A1900"> * name.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><fstream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdio.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdlib.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string.h></span>
<b><span style="color:#0000FF">extern</span></b> <span style="color:#FF0000">"C"</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><mkdio.h></span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">**</span>argv<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> i<span style="color:#990000">,</span> inidx<span style="color:#990000">,</span> outidx<span style="color:#990000">;</span>
<span style="color:#008080">FILE</span> <span style="color:#990000">*</span>fpin<span style="color:#990000">,</span> <span style="color:#990000">*</span>fpout<span style="color:#990000">;</span>
<span style="color:#009900">char</span> <span style="color:#990000">*</span>css <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// check command line parameters</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">],</span><span style="color:#FF0000">"-css"</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>argc <span style="color:#990000">!=</span> <span style="color:#993399">4</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>argc <span style="color:#990000">!=</span> <span style="color:#993399">5</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Usage: "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" [-css <css_file>] <input_file> <output_file>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
css <span style="color:#990000">=</span> argv<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">];</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>argc <span style="color:#990000">!=</span> <span style="color:#993399">2</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>argc <span style="color:#990000">!=</span> <span style="color:#993399">3</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Usage: "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" [-css <css_file>] <input_file> <output_file>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// if the -css flag is there, then the input and output file names will be in a different place</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> css <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
inidx <span style="color:#990000">=</span> <span style="color:#993399">3</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> argc <span style="color:#990000">==</span> <span style="color:#993399">4</span> <span style="color:#990000">)</span>
outidx <span style="color:#990000">=</span> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b>
outidx <span style="color:#990000">=</span> <span style="color:#993399">4</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
inidx <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> argc <span style="color:#990000">==</span> <span style="color:#993399">2</span> <span style="color:#990000">)</span>
outidx <span style="color:#990000">=</span> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b>
outidx <span style="color:#990000">=</span> <span style="color:#993399">2</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// try to open the file</span></i>
<span style="color:#008080">ifstream</span> <b><span style="color:#000000">fin</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>inidx<span style="color:#990000">]);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>fin<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error: unable to open input file: "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// read in the file into an internal string</span></i>
<span style="color:#009900">bool</span> codemode <span style="color:#990000">=</span> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<span style="color:#008080">string</span> codeprefix <span style="color:#990000">=</span> <span style="color:#FF0000">" "</span><span style="color:#990000">;</span>
<span style="color:#008080">string</span> file<span style="color:#990000">,</span> line<span style="color:#990000">,</span> firstline<span style="color:#990000">;</span>
<span style="color:#009900">bool</span> gotfirstline <span style="color:#990000">=</span> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span> <b><span style="color:#000000">getline</span></b><span style="color:#990000">(</span>fin<span style="color:#990000">,</span>line<span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>gotfirstline <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
gotfirstline <span style="color:#990000">=</span> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
firstline <span style="color:#990000">=</span> line<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'`'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'`'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'`'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// we ignore any language specification after the three back quotes</span></i>
codemode <span style="color:#990000">=</span> <span style="color:#990000">!</span>codemode<span style="color:#990000">;</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> codemode <span style="color:#990000">)</span>
file <span style="color:#990000">+=</span> codeprefix<span style="color:#990000">;</span>
file <span style="color:#990000">+=</span> line <span style="color:#990000">+</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// try to process the string</span></i>
MMIOT<span style="color:#990000">*</span> doc <span style="color:#990000">=</span> <b><span style="color:#000000">mkd_string</span></b><span style="color:#990000">(</span>file<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">(),</span>file<span style="color:#990000">.</span><b><span style="color:#000000">length</span></b><span style="color:#990000">(),</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> doc <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error processing markdown input file in mkd_in()"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// determine where the output is going</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>outidx <span style="color:#990000">==</span> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">)</span> <span style="color:#990000">||</span> <span style="color:#990000">(!</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>outidx<span style="color:#990000">],</span><span style="color:#FF0000">"-"</span><span style="color:#990000">))</span> <span style="color:#990000">)</span>
fpout <span style="color:#990000">=</span> stdout<span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>fpout <span style="color:#990000">=</span> <b><span style="color:#000000">fopen</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>outidx<span style="color:#990000">],</span><span style="color:#FF0000">"w"</span><span style="color:#990000">))</span> <span style="color:#990000">==</span> NULL <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Error: unable to open output file: %s</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> argv<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">]);</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// write the output</span></i>
<b><span style="color:#000000">fprintf</span></b> <span style="color:#990000">(</span>fpout<span style="color:#990000">,</span> <span style="color:#FF0000">"<!doctype html></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"><html></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"><head></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"><meta charset=</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">utf-8</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"><title>%s</title></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> firstline<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> css <span style="color:#990000">)</span>
<b><span style="color:#000000">fprintf</span></b> <span style="color:#990000">(</span>fpout<span style="color:#990000">,</span> <span style="color:#FF0000">"<link href=</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">%s</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000"> media=</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">all</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000"> rel=</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">stylesheet</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000"> type=</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">text/css</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">>"</span><span style="color:#990000">,</span> css<span style="color:#990000">);</span>
<b><span style="color:#000000">fprintf</span></b> <span style="color:#990000">(</span>fpout<span style="color:#990000">,</span> <span style="color:#FF0000">"</head></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"><body></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">);</span>
<b><span style="color:#000000">markdown</span></b> <span style="color:#990000">(</span>doc<span style="color:#990000">,</span> fpout<span style="color:#990000">,</span> <span style="color:#993399">0</span><span style="color:#990000">);</span>
<b><span style="color:#000000">fprintf</span></b> <span style="color:#990000">(</span>fpout<span style="color:#990000">,</span> <span style="color:#FF0000">"</body></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000"></html></span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">);</span>
<b><span style="color:#000000">fclose</span></b><span style="color:#990000">(</span>fpout<span style="color:#990000">);</span>
<b><span style="color:#000000">mkd_cleanup</span></b><span style="color:#990000">(</span>doc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>