public
Description: An implementation of markdown in C, using a PEG grammar
Clone URL: git://github.com/jgm/peg-markdown.git
commit  f21b1c1af65f1668a7e7cb4b0d56969604b645a0
tree    9045d8104962f7a015b11e1c47404b4883eaf7bd
parent  2dfff24fb877bca11efe2cf91316f4abd3ef016a
peg-markdown / markdown.c
100644 176 lines (143 sloc) 5.978 kb
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
/**********************************************************************
 
markdown.c - markdown in C using a PEG grammar.
(c) 2008 John MacFarlane (jgm at berkeley dot edu).
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
***********************************************************************/
 
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <glib.h>
#include "markdown_peg.h"
 
static int extensions;
 
/**********************************************************************
 
Main program and auxiliary functions.
 
Reads input from files on command line, or from stdin
if no arguments given. Converts tabs to spaces using TABSTOP.
Parses the result for references (References), and then again for
conversion to HTML (Doc). The parser actions print the converted
HTML, so there is no separate printing step here. Character encodings
are ignored.
 
***********************************************************************/
 
#define VERSION "0.2.3"
#define COPYRIGHT "Copyright (c) 2008 John MacFarlane.\n" \
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" \
                  "This is free software: you are free to change and redistribute it.\n" \
                  "There is NO WARRANTY, to the extent permitted by law."
 
/* print version and copyright information */
void version(char *progname)
{
  printf("%s version %s\n"
         "%s\n",
         progname,
         VERSION,
         COPYRIGHT);
}
 
int main(int argc, char * argv[]) {
 
    int numargs; /* number of filename arguments */
    FILE *inputs[argc]; /* array of file pointers for inputs */
    int lastinp; /* index of last file in inputs */
    int i;
 
    GString *inputbuf;
 
    FILE *input;
    FILE *output;
    char curchar;
    char *progname = argv[0];
    /* the output filename is initially 0 (a.k.a. stdout) */
 
    int output_format = HTML_FORMAT;
 
    static gboolean opt_version = FALSE;
    static gchar *opt_output = 0;
    static gchar *opt_to = 0;
    static gboolean opt_smart = FALSE;
    static gboolean opt_notes = FALSE;
    static gboolean opt_allext = FALSE;
 
    static GOptionEntry entries[] =
    {
      { "version", 'v', 0, G_OPTION_ARG_NONE, &opt_version, "print version and exit", NULL },
      { "output", 'o', 0, G_OPTION_ARG_STRING, &opt_output, "send output to FILE (default is stdout)", "FILE" },
      { "to", 't', 0, G_OPTION_ARG_STRING, &opt_to, "convert to FORMAT (default is html)", "FORMAT" },
      { "extensions", 'x', 0, G_OPTION_ARG_NONE, &opt_allext, "use all syntax extensions", NULL },
      { NULL }
    };
 
    static GOptionEntry ext_entries[] =
    {
      { "smart", 0, 0, G_OPTION_ARG_NONE, &opt_smart, "use smart typography extension", NULL },
      { "notes", 0, 0, G_OPTION_ARG_NONE, &opt_notes, "use notes extension", NULL },
      { NULL }
    };
 
    GError *error = NULL;
    GOptionContext *context;
    GOptionGroup *ext_group;
 
    context = g_option_context_new ("- convert markdown-formatted text");
    g_option_context_add_main_entries (context, entries, NULL);
    ext_group = g_option_group_new ("extensions", "Syntax extensions", "show available syntax extensions", NULL, NULL);
    g_option_group_add_entries (ext_group, ext_entries);
    g_option_context_add_group (context, ext_group);
    g_option_context_set_description (context, "Converts text in specified files (or stdin) from markdown to FORMAT.\n"
                                               "Available FORMATs: html, latex, groff-mm");
    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("option parsing failed: %s\n", error->message);
        exit (1);
    }
 
    if (opt_version) {
        version(progname);
        return EXIT_SUCCESS;
    }
 
    extensions = 0;
    if (opt_allext)
        extensions = 0xFFFFFF; /* turn on all extensions */
    if (opt_smart)
        extensions = extensions | EXT_SMART;
    if (opt_notes)
        extensions = extensions | EXT_NOTES;
 
    if (opt_to == NULL)
        output_format = HTML_FORMAT;
    else if (strcmp(opt_to, "html") == 0)
        output_format = HTML_FORMAT;
    else if (strcmp(opt_to, "latex") == 0)
        output_format = LATEX_FORMAT;
    else if (strcmp(opt_to, "groff-mm") == 0)
        output_format = GROFF_MM_FORMAT;
    else {
        fprintf(stderr, "%s: Unknown output format '%s'\n", progname, opt_to);
        exit(EXIT_FAILURE);
    }
 
    /* we allow "-" as a synonym for stdout here */
    if (opt_output == NULL || strcmp(opt_output, "-") == 0)
        output = stdout;
    else if (!(output = fopen(opt_output, "w"))) {
        perror(opt_output);
        return 1;
    }
        
    numargs = argc - 1;
    if (numargs == 0) { /* use stdin if no files specified */
       inputs[0] = stdin;
       lastinp = 0;
    }
    else { /* open all the files on command line */
       for (i = 0; i < numargs; i++)
            if ((inputs[i] = fopen(argv[i+1], "r")) == NULL) {
                perror(argv[i+1]);
                exit(EXIT_FAILURE);
            }
       lastinp = i - 1;
    }
 
    inputbuf = g_string_new("");
   
    for (i=0; i <= lastinp; i++) {
        input = inputs[i];
        while ((curchar = fgetc(input)) != EOF)
            g_string_append_c(inputbuf, curchar);
        fclose(input);
    }
 
    markdown_to_stream(inputbuf->str, extensions, output_format, output);
    fprintf(output, "\n");
    g_string_free(inputbuf, true);
 
    return(EXIT_SUCCESS);
}