xref: /Universal-ctags/parsers/frontmatter.c (revision 532866040b26c111e1e2a75f9383272afb02abfe)
1 /*
2 *
3 *   Copyright (c) 2022, Masatake YAMATO
4 *   Copyright (c) 2022, Red Hat, K.K.
5 *
6 *   This source code is released for free distribution under the terms of the
7 *   GNU General Public License version 2 or (at your option) any later version.
8 *
9 * This module contains functions for extracting language objects in FrontMatter.
10 *
11 * https://gohugo.io/content-management/front-matter
12 */
13 
14 /*
15 *   INCLUDE FILES
16 */
17 #include "general.h"	/* must always come first */
18 
19 #include "frontmatter.h"
20 
21 #include "entry.h"
22 #include "parse.h"
23 #include "promise.h"
24 #include "read.h"
25 
26 #include <string.h>
27 
28 /*
29 *   DATA DEFINITIONS
30 */
31 static kindDefinition FrontMatterKinds [] = {
32 	{ true, 't', "title", "titles", },
33 };
34 
35 /*
36 *   FUNCTION DEFINITIONS
37 */
findFrontMatterTags(void)38 static void findFrontMatterTags (void)
39 {
40 	const unsigned char *line = readLineFromInputFile ();
41 
42 	if (line == NULL)
43 		return;
44 
45 #ifdef HAVE_LIBYAML
46 	if (strcmp("---", (const char *)line) == 0)
47 	{
48 		line = readLineFromInputFile ();
49 		unsigned long endOffset = strlen((const char *)line);
50 		if (line)
51 		{
52 			long startLineNum = getInputLineNumber ();
53 			while ((line = readLineFromInputFile()))
54 				endOffset = strlen((const char *)line);
55 
56 			long endLineNum = getInputLineNumber ();
57 
58 			makePromise ("YamlFrontMatter", startLineNum, 0,
59 						 endLineNum, endOffset, startLineNum);
60 		}
61 		return;
62 	}
63 #endif
64 }
65 
FrontMatterParser(void)66 extern parserDefinition* FrontMatterParser (void)
67 {
68 	parserDefinition* def = parserNew ("FrontMatter");
69 	def->kindTable      = FrontMatterKinds;
70 	def->kindCount  = ARRAY_SIZE (FrontMatterKinds);
71 
72 	def->parser     = findFrontMatterTags;
73 
74 	return def;
75 }
76