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