public
Description: An implementation of markdown in C, using a PEG grammar
Clone URL: git://github.com/jgm/peg-markdown.git
jgm (author)
Tue Jun 17 09:49:02 -0700 2008
commit  a5937cf8ec3db3ad43354b68ef4b56793600f0fb
tree    21c3f2e357754ad754aaabfbe874b1e32013805a
parent  fb8d07d70bc1b64f3acfbd3b57f3c1d10abd073f
peg-markdown / markdown.c
100644 177 lines (142 sloc) 6.06 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
177
/**********************************************************************
 
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;
 
/**********************************************************************
 
The main program is just a wrapper around the library functions in
markdown_lib.c. It parses command-line options, reads the text to
be converted from input files or stdin, converts the text, and sends
the output to stdout or a file. Character encodings are ignored.
 
***********************************************************************/
 
#define VERSION "0.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(const char *progname)
{
  printf("%s version %s\n"
         "%s\n",
         progname,
         VERSION,
         COPYRIGHT);
}
 
int main(int argc, char * argv[]) {
 
    int numargs; /* number of filename arguments */
    int i;
 
    GString *inputbuf;
    char *out; /* string containing processed output */
 
    FILE *input;
    FILE *output;
    char curchar;
    char *progname = argv[0];
 
    int output_format = HTML_FORMAT;
 
    /* Code for command-line option parsing. */
 
    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 }
    };
 
    /* Options to active syntax extensions. These appear separately in --help. */
    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 ("[FILE...]");
    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);
    }
    g_option_context_free(context);
 
    /* Process command-line options and arguments. */
 
    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;
    }
 
    inputbuf = g_string_new(""); /* string for concatenated input */
 
    /* Read input from stdin or input files into inputbuf */
 
    numargs = argc - 1;
    if (numargs == 0) { /* use stdin if no files specified */
        while ((curchar = fgetc(stdin)) != EOF)
            g_string_append_c(inputbuf, curchar);
        fclose(stdin);
    }
    else { /* open all the files on command line */
       for (i = 0; i < numargs; i++) {
            if ((input = fopen(argv[i+1], "r")) == NULL) {
                perror(argv[i+1]);
                exit(EXIT_FAILURE);
            }
            while ((curchar = fgetc(input)) != EOF)
                g_string_append_c(inputbuf, curchar);
            fclose(input);
       }
    }
 
    out = markdown_to_string(inputbuf->str, extensions, output_format);
    fprintf(output, "%s\n", out);
    free(out);
 
    g_string_free(inputbuf, true);
 
    return(EXIT_SUCCESS);
}