forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
markdown.c.html
96 lines (94 loc) · 14.9 KB
/
markdown.c.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
<!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.c</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. It requires the</span></i>
<i><span style="color:#9A1900"> * libmarkdown2-dev pacakage be installed under 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: "gcc markdown.c -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"><stdio.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><mkdio.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>
<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>
<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>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Usage: %s [-css <css_file>] <input_file> <output_file></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">0</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>
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>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Usage: %s [-css <css_file>] <input_file> <output_file></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">0</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>
<span style="color:#FF0000">}</span>
<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>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>fpin <span style="color:#990000">=</span> <b><span style="color:#000000">fopen</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>inidx<span style="color:#990000">],</span><span style="color:#FF0000">"r"</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 input 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">1</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>
<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>
MMIOT<span style="color:#990000">*</span> doc <span style="color:#990000">=</span> <b><span style="color:#000000">mkd_in</span></b><span style="color:#990000">(</span>fpin<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>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Error processing markdown input file in mkd_in()</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</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>
<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">"</span><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>