1 /*
2 *
3 *   Copyright (c) 2009, Jon Strait
4 *
5 *   This source code is released for free distribution under the terms of the
6 *   GNU General Public License.
7 *
8 *   This module contains functions for generating tags for Markdown files.
9 */
10 
11 /*
12 *   INCLUDE FILES
13 */
14 #include "general.h"	/* must always come first */
15 
16 #include <ctype.h>
17 #include <string.h>
18 
19 #include "parse.h"
20 #include "read.h"
21 #include "vstring.h"
22 #include "routines.h"
23 #include "entry.h"
24 
25 /*
26 *   DATA DEFINITIONS
27 */
28 
29 static kindDefinition MarkdownKinds[] = {
30 	{ true, 'v', "variable", "sections" }
31 };
32 
33 /*
34 *   FUNCTION DEFINITIONS
35 */
36 
37 /* checks if str is all the same character */
issame(const char * str)38 static bool issame(const char *str)
39 {
40 	char first = *str;
41 
42 	while (*(++str))
43 	{
44 		if (*str && *str != first)
45 			return false;
46 	}
47 	return true;
48 }
49 
makeMarkdownTag(const vString * const name,bool name_before)50 static void makeMarkdownTag (const vString* const name, bool name_before)
51 {
52 	tagEntryInfo e;
53 	initTagEntry (&e, vStringValue(name), 0);
54 
55 	if (name_before)
56 		e.lineNumber--;	/* we want the line before the underline chars */
57 
58 	makeTagEntry(&e);
59 }
60 
61 
findMarkdownTags(void)62 static void findMarkdownTags (void)
63 {
64 	vString *name = vStringNew();
65 	const unsigned char *line;
66 
67 	while ((line = readLineFromInputFile()) != NULL)
68 	{
69 		int name_len = vStringLength(name);
70 
71 		/* underlines must be the same length or more */
72 		if (name_len > 0 &&	(line[0] == '=' || line[0] == '-') && issame((const char*) line))
73 		{
74 			makeMarkdownTag(name, true);
75 		}
76 		else if (line[0] == '#') {
77 			vStringClear(name);
78 			vStringCatS(name, (const char *) line);
79 			makeMarkdownTag(name, false);
80 		}
81 		else {
82 			vStringClear (name);
83 			if (! isspace(*line))
84 				vStringCatS(name, (const char*) line);
85 		}
86 	}
87 	vStringDelete (name);
88 }
89 
MarkdownParser(void)90 extern parserDefinition* MarkdownParser (void)
91 {
92 	static const char *const patterns [] = { "*.md", NULL };
93 	static const char *const extensions [] = { "md", NULL };
94 	parserDefinition* const def = parserNew ("Markdown");
95 
96 	def->kindTable = MarkdownKinds;
97 	def->kindCount = ARRAY_SIZE (MarkdownKinds);
98 	def->patterns = patterns;
99 	def->extensions = extensions;
100 	def->parser = findMarkdownTags;
101 	return def;
102 }
103 
104