1 # mdcode: extract C code from a _markdown_ file.
3 _markdown_ is a popular format for simple text markup which can easily
4 be converted to HTML. As it allows easy indication of sections of
5 code, it is quite suitable for use in literate programming. This file
6 is an example of that usage.
8 The code included below provides two related functionalities.
9 Firstly it provides a library routine for extracting code out of a
10 _markdown_ file, so that other routines might make use of it.
12 Secondly it provides a simple client of this routine which extracts
13 1 or more C-language files from a markdown document so they can be
14 passed to a C compiler. These two combined to make a tool that is needed
15 to compile this tool. Yes, this is circular. A prototype tool was
16 used for the first extraction.
18 The tool provided is described as specific to the C language as it
21 ##### Example: a _line_ command
23 #line __line-number__ __file-name__
25 lines so that the C compiler will report where in the markdown file
26 any error is found. This tool is suitable for any other language
27 which allows the same directive, or will treat it as a comment.
31 Literate programming is more than just including comments with the
32 code, even nicely formatted comments. It also involves presenting the
33 code in an order that makes sense to a human, rather than an order
34 that makes sense to a compiler. For this reason a core part of any
35 literate programming tool is the ability to re-arrange the code found
36 in the document into a different order in the final code file - or
37 files. This requires some form of linkage to be encoded.
39 The approach taken here is focused around section headings - of any
42 All the code in any section is treated as a single sequential
43 collection of code, and is named by the section that it is in. If
44 multiple sections have the same name, then the code blocks in all of
45 them are joined together in the order they appear in the document.
47 A code section can contain a special marker which starts with 2
49 The text after the marker must be the name of some section which
50 contains code. Code from that section will be interpolated in place
51 of the marker, and will be indented to match the indent of the marker.
53 It is not permitted for the same code to be interpolated multiple
54 times. Allowing this might make some sense, but it is probably a
55 mistake, and prohibiting it make some of the code a bit cleaner.
57 Equally, every section of code should be interpolated at least once -
58 with one exception. This exception is imposed by the
59 tool, not the library. A different client could impose different
60 rules on the names of top-level code sections.
62 One example of the exception we have already seen. A section name
63 starting __Example:__ indicates code that is not to be included in the
64 final product. Any leading word will do, providing there is a space,
65 and the first space is preceded by a colon, that section name will be
68 A special case of this exception exists for the leading word
69 __File__. These sections are the top level code sections and they
70 will be written to the named file. Thus a section named
71 __File: foo__ should not be referenced by another section, and its
72 contents after all references are expanded will be written to the file
75 Any section containing code that does not start __Word:__
76 must be included in some other section exactly once.
80 Allowing multiple top level code sections which name different files
81 means that one _markdown_ document can describe several files. This
82 is very useful with the C language where a program file and a header
83 file might be related. For the present document we will have a header
84 file and two code files, one with the library content and one for the
87 It will also be very convenient to create a `makefile` fragment to
88 ensure the code is compiled correctly. A simple `make -f mdcode.mk`
89 will "do the right thing".
95 mdcode.h libmdcode.c md2c.c mdcode.mk : mdcode.mdc
103 ## exported functions
105 ### File: libmdcode.c
114 ## internal functions
119 libmdcode.o : libmdcode.c mdcode.h
120 $(CC) $(CFLAGS) -c libmdcode.c
137 md2c : md2c.o libmdcode.o
138 $(CC) $(CFLAGS) -o md2c md2c.o libmdcode.o
139 md2c.o : md2c.c mdcode.h
140 $(CC) $(CFLAGS) -c md2c.c
144 As the core purpose of _mdcode_ is to discover and re-arrange blocks
145 of text, it makes sense to map the whole document file into memory and
146 produce a data structure which lists various parts of the file in the
147 appropriate order. Each node in this structure will have some text
148 from the document, a child pointer, and a next pointer, any of which
149 might not be present. The text is most easily stored as a pointer and a
150 length. We'll call this a `text`
152 A list of these `code_nodes` will belong to each section and it will
153 be useful to have a separate `section` data structure to store the
154 list of `code_nodes`, the section name, and some other information.
156 This other information will include a reference counter so we can
157 ensure proper referencing, and an `indent` depth. As referenced
158 content can have an extra indent added, we need to know what that is.
159 The `code_node` will also have an `indent` depth which eventually gets
160 set to the sum for the indents from all references on the path from
163 Finally we need to know if the `code_node` was recognised by being
164 indented or not. If it was, the client of this data will want to
165 strip of the leading tab or 4 spaces. Hence a `needs_strip` flag is
177 struct code_node *code;
178 struct section *next;
186 struct code_node *next;
187 struct section *child;
194 struct code_node *last;
199 You will note that the `struct psection` contains an anonymous `struct
200 section` embedded at the start. To make this work right, GCC
201 requires the `-fplan9-extensions` flag.
203 ##### File: mdcode.mk
205 CFLAGS += -fplan9-extensions
207 ### Manipulating the node
209 Though a tree with `next` and `child` links is the easiest way to
210 assemble the various code sections, it is not the easiest form for
211 using them. For that a simple list would be best.
213 So once we have a fully linked File section we will want to linearize
214 it, so that the `child` links become `NULL` and the `next` links will
215 find everything required. It is at this stage that the requirements
216 that each section is linked only once becomes import.
218 `code_linearize` will merge the `code_node`s from any child into the
219 given `code_node`. As it does this it sets the 'indent' field for
222 Note that we don't clear the section's `last` pointer, even though
223 it no longer owns any code. This allows subsequent code to see if a
224 section ever had any code, and to report an error if a section is
225 referenced but not defined.
227 ##### internal functions
229 static void code_linearize(struct code_node *code)
232 for (t = code; t; t = t->next)
234 for (; code; code = code->next)
236 struct code_node *next = code->next;
237 struct psection *pchild =
238 (struct psection *)code->child;
239 int indent = pchild->indent;
240 code->next = code->child->code;
241 code->child->code = NULL;
243 for (t = code; t->next; t = t->next)
244 t->next->indent = code->indent + indent;
249 Once a client has made use of a linearized code set, it will probably
252 void code_free(struct code_node *code)
255 struct code_node *this;
257 code_linearize(code);
264 ##### exported functions
266 void code_free(struct code_node *code);
268 ### Building the tree
270 As we parse the document there are two things we will want to do to
271 node trees: add some text or add a reference. We'll assume for now
272 that the relevant section structures have been found, and will just
273 deal with the `code_node`.
275 Adding text simply means adding another node. We will never have
276 empty nodes, even if the last node only has a child, new text must go
279 ##### internal functions
281 static void code_add_text(struct psection *where, struct text txt,
282 int line_no, int needs_strip)
287 n = malloc(sizeof(*n));
290 n->line_no = line_no;
291 n->needs_strip = needs_strip;
295 where->last->next = n;
301 However when adding a link, we might be able to include it in the last
302 `code_node` if it currently only has text.
304 void code_add_link(struct psection *where, struct psection *to,
310 to->refcnt++; // this will be checked elsewhere
311 if (where->last && where->last->child == NULL) {
312 where->last->child = to;
315 n = malloc(sizeof(*n));
322 where->last->next = n;
330 Now we need a lookup table to be able to find sections by name.
331 Something that provides an `n*log(N)` search time is probably
332 justified, but for now I want a minimal stand-alone program so a
333 linked list managed by insertion-sort will do.
335 The text compare function will likely be useful for any clients of our
336 library, so we may as well export it.
338 If we cannot find a section, we simply want to create it. This allows
339 sections and references to be created in any order. Sections with
340 no references or no content will cause a warning eventually.
342 #### exported functions
344 int text_cmp(struct text a, struct text b);
346 #### internal functions
348 int text_cmp(struct text a, struct text b)
353 int cmp = strncmp(a.txt, b.txt, len);
357 return a.len - b.len;
360 static struct psection *section_find(struct psection **list, struct text name)
362 struct psection *new;
364 int cmp = text_cmp((*list)->section, name);
369 list = (struct psection **)&((*list)->next);
371 /* Add this section */
372 new = malloc(sizeof(*new));
383 ## Parsing the _markdown_
385 Parsing markdown is fairly easy, though there are complications.
387 The document is divided into "paragraphs" which are mostly separated by blank
388 lines (which may contain white space). The first few characters of
389 the first line of a paragraph determine the type of paragraph. For
390 our purposes we are only interested in list paragraphs, code
391 paragraphs, section headings, and everything else. Section headings
392 are single-line paragraphs and so do not require a preceding or
393 following blank line.
395 Section headings start with 1 or more hash characters (__#__). List
396 paragraphs start with hyphen, asterisk, plus, or digits followed by a
397 period. Code paragraphs aren't quite so easy.
399 The "standard" code paragraph starts with 4 or more spaces, or a tab.
400 However if the previous paragraph was a list paragraph, then those
401 spaces indicate another paragraph in the same list item, and 8 or
402 more spaces are required. Unless a nested list is in effect, in
403 which case 12 or more are need. Unfortunately not all _markdown_
404 parsers agree on nested lists.
406 Two alternate styles for marking code are in active use. "Github" uses
407 three backticks(_`` ``` ``_), while "pandoc" uses three or more tildes
408 (_~~~_). In these cases the code should not be indented.
410 Trying to please everyone as much as possible, this parser will handle
411 everything except for code inside lists.
413 So an indented (4+) paragraph after a list paragraph is always a list
414 paragraph, otherwise it is a code paragraph. A paragraph that starts
415 with three backticks or three tildes is code which continues until a
416 matching string of backticks or tildes.
420 While walking the document looking for various markers we will *not*
421 use the `struct text` introduced earlier as advancing that requires
422 updating both start and length which feels clumsy. Instead we will
423 carry `pos` and `end` pointers, only the first of which needs to
426 So to start, we need to skip various parts of the document. `lws`
427 stands for "Linear White Space" and is a term that comes from the
428 Email RFCs (e.g. RFC822). `line` and `para` are self explanatory.
429 Note that `skip_para` needs to update the current line number.
430 `skip_line` doesn't but every caller should.
432 #### internal functions
434 static char *skip_lws(char *pos, char *end)
436 while (pos < end && (*pos == ' ' || *pos == '\t'))
441 static char *skip_line(char *pos, char *end)
443 while (pos < end && *pos != '\n')
450 static char *skip_para(char *pos, char *end, int *line_no)
452 /* Might return a pointer to a blank line, as only
453 * one trailing blank line is skipped
456 pos = skip_line(pos, end);
462 *(pos = skip_lws(pos, end)) != '\n') {
463 pos = skip_line(pos, end);
466 if (pos < end && *pos == '\n') {
473 ### Recognising things
475 Recognising a section header is trivial and doesn't require a
476 function. However we need to extract the content of a section header
477 as a `struct text` for passing to `section_find`.
478 Recognising the start of a new list is fairly easy. Recognising the
479 start (and end) of code is a little messy so we provide a function for
480 matching the first few characters, which has a special case for "4
483 #### internal includes
488 #### internal functions
490 static struct text take_header(char *pos, char *end)
494 while (pos < end && *pos == '#')
496 while (pos < end && *pos == ' ')
499 while (pos < end && *pos != '\n')
501 while (pos > section.txt &&
502 (pos[-1] == '#' || pos[-1] == ' '))
504 section.len = pos - section.txt;
508 static int is_list(char *pos, char *end)
510 if (strchr("-*+", *pos))
513 while (pos < end && isdigit(*pos))
515 if (pos < end && *pos == '.')
521 static int matches(char *start, char *pos, char *end)
524 return matches("\t", pos, end) ||
525 matches(" ", pos, end);
526 return (pos + strlen(start) < end &&
527 strncmp(pos, start, strlen(start)) == 0);
530 ### Extracting the code
532 Now that we can skip paragraphs and recognise what type each paragraph
533 is, it is time to parse the file and extract the code. We'll do this
534 in two parts, first we look at what to do with some code once we
535 find it, and then how to actually find it.
537 When we have some code, we know where it is, what the end marker
538 should look like, and which section it is in.
540 There are two sorts of end markers: the presence of a particular
541 string, or the absence of an indent. We will use a string to
542 represent a presence, and a `NULL` to represent the absence.
544 While looking at code we don't think about paragraphs are all - just
545 look for a line that starts with the right thing.
546 Every line that is still code then needs to be examined to see if it
547 is a section reference.
549 When a section reference is found, all preceding code (if any) must be
550 added to the current section, then the reference is added.
552 When we do find the end of the code, all text that we have found but
553 not processed needs to be saved too.
555 When adding a reference we need to set the `indent`. This is the
556 number of spaces (counting 8 for tabs) after the natural indent of the
557 code (which is a tab or 4 spaces). We use a separate function `count_spaces`
560 #### internal functions
562 static int count_space(char *sol, char *p)
576 static char *take_code(char *pos, char *end, char *marker,
577 struct psection **table, struct text section,
581 int line_no = *line_nop;
582 int start_line = line_no;
583 struct psection *sect;
585 sect = section_find(table, section);
591 if (marker && matches(marker, pos, end))
594 (skip_lws(pos, end))[0] != '\n' &&
595 !matches(NULL, pos, end))
596 /* Paragraph not indented */
599 /* Still in code - check for reference */
604 else if (strcmp(sol, " ") == 0)
607 t = skip_lws(sol, end);
608 if (t[0] != '#' || t[1] != '#') {
609 /* Just regular code here */
610 pos = skip_line(sol, end);
618 txt.len = pos - start;
619 code_add_text(sect, txt, start_line,
622 ref = take_header(t, end);
624 struct psection *refsec = section_find(table, ref);
625 code_add_link(sect, refsec, count_space(sol, t));
627 pos = skip_line(t, end);
630 start_line = line_no;
635 txt.len = pos - start;
636 code_add_text(sect, txt, start_line,
640 pos = skip_line(pos, end);
649 It is when looking for the code that we actually use the paragraph
650 structure. We need to recognise section headings so we can record the
651 name, list paragraphs so we can ignore indented follow-on paragraphs,
652 and the three different markings for code.
654 #### internal functions
656 static struct psection *code_find(char *pos, char *end)
658 struct psection *table = NULL;
661 struct text section = {0};
665 section = take_header(pos, end);
667 pos = skip_line(pos, end);
669 } else if (is_list(pos, end)) {
671 pos = skip_para(pos, end, &line_no);
672 } else if (!in_list && matches(NULL, pos, end)) {
673 pos = take_code(pos, end, NULL, &table,
675 } else if (matches("```", pos, end)) {
677 pos = skip_line(pos, end);
679 pos = take_code(pos, end, "```", &table,
681 } else if (matches("~~~", pos, end)) {
683 pos = skip_line(pos, end);
685 pos = take_code(pos, end, "~~~", &table,
690 pos = skip_para(pos, end, &line_no);
696 ### Returning the code
698 Having found all the code blocks and gathered them into a list of
699 section, we are now ready to return them to the caller. This is where
700 to perform consistency checks, like at most one reference and at least
701 one definition for each section.
703 All the sections with no references are returned in a list for the
704 caller to consider. The are linearized first so that the substructure
705 is completely hidden -- except for the small amount of structure
706 displayed in the line numbers.
708 To return errors, we have the caller pass a function which takes an
709 error message - a `code_err_fn`.
713 typedef void (*code_err_fn)(char *msg);
715 #### internal functions
716 struct section *code_extract(char *pos, char *end, code_err_fn error)
718 struct psection *table;
719 struct section *result = NULL;
720 struct section *tofree = NULL;
722 table = code_find(pos, end);
725 struct psection *t = (struct psection*)table->next;
726 if (table->last == NULL) {
729 "Section \"%.*s\" is referenced but not declared",
730 table->section.len, table->section.txt);
734 if (table->refcnt == 0) {
735 /* Root-section, return it */
736 table->next = result;
738 code_linearize(result->code);
740 table->next = tofree;
742 if (table->refcnt > 1) {
745 "Section \"%.*s\" referenced multiple times (%d).",
746 table->section.len, table->section.txt,
755 struct section *t = tofree->next;
762 ##### exported functions
764 struct section *code_extract(char *pos, char *end, code_err_fn error);
769 Now that we can extract code from a document and link it all together
770 it is time to do something with that code. Firstly we need to print
773 ### Printing the Code
775 Printing is mostly straight forward - we just walk the list and print
776 the code sections, adding whatever indent is required for each line.
777 However there is a complication (isn't there always)?
779 For code that was recognised because the paragraph was indented, we
780 need to strip that indent first. For other code, we don't.
782 The approach taken here is simple, though it could arguably be wrong
783 in some unlikely cases. So it might need to be fixed later.
785 If the first line of a code block is indented, then either one tab or
786 4 spaces are striped from every non-blank line.
788 This could go wrong if the first line of a code block marked by
789 _`` ``` ``_ is indented. To overcome this we would need to
790 record some extra state in each `code_node`. For now we won't bother.
792 The indents we insert will all be spaces. This might not work well
795 ##### internal functions
797 void code_node_print(FILE *out, struct code_node *node,
800 for (; node; node = node->next) {
801 char *c = node->code.txt;
802 int len = node->code.len;
807 fprintf(out, "#line %d \"%s\"\n",
808 node->line_no, fname);
810 fprintf(out, "%*s", node->indent, "");
811 if (node->needs_strip) {
812 if (*c == '\t' && len > 1) {
815 } else if (strncmp(c, " ", 4) == 0 && len > 4) {
824 } while (len && c[-1] != '\n');
829 ###### exported functions
830 void code_node_print(FILE *out, struct code_node *node, char *fname);
832 ### Bringing it all together
834 We are just about ready for the `main` function of the tool which will
835 extract all this lovely code and compile it. Just one helper is still
838 #### Handling filenames
840 Section names are stored in `struct text` which is not `nul`
841 terminated. Filenames passed to `open` need to be null terminated.
842 So we need to convert one to the other, and strip the leading `File:`
843 of while we are at it.
845 ##### client functions
847 static void copy_fname(char *name, int space, struct text t)
852 if (len < 5 || strncmp(sec, "File:", 5) != 0)
856 while (len && sec[0] == ' ') {
862 strncpy(name, sec, len);
868 And now we take a single file name, extract the code, and if there are
869 no error we write out a file for each appropriate code section. And
873 ##### client includes
877 #include <sys/mman.h>
880 ##### client functions
883 static void pr_err(char *msg)
886 fprintf(stderr, "%s\n", msg);
889 static char *strnchr(char *haystack, int len, char needle)
891 while (len > 0 && *haystack && *haystack != needle) {
895 return len > 0 && *haystack == needle ? haystack : NULL;
898 int main(int argc, char *argv[])
903 struct section *table, *s, *prev;
907 fprintf(stderr, "Usage: mdcode file.mdc\n");
910 fd = open(argv[1], O_RDONLY);
912 fprintf(stderr, "mdcode: cannot open %s: %s\n",
913 argv[1], strerror(errno));
916 len = lseek(fd, 0, 2);
917 file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
918 table = code_extract(file, file+len, pr_err);
921 (code_free(s->code), prev = s, s = s->next, free(prev))) {
924 char *spc = strnchr(s->section.txt, s->section.len, ' ');
926 if (spc > s->section.txt && spc[-1] == ':' &&
927 strncmp(s->section.txt, "File: ", 6) != 0)
928 /* Ignore this section */
930 if (strncmp(s->section.txt, "File: ", 6) != 0) {
931 fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n",
932 s->section.len, s->section.txt);
936 copy_fname(fname, sizeof(fname), s->section);
938 fprintf(stderr, "Missing file name at:%.*s\n",
939 s->section.len, s->section.txt);
943 fl = fopen(fname, "w");
945 fprintf(stderr, "Cannot create %s: %s\n",
946 fname, strerror(errno));
950 code_node_print(fl, s->code, argv[1]);