forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquarto.c
222 lines (181 loc) · 4.83 KB
/
quarto.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
*
* Copyright (c) 2023, Masatake YAMATO
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for Quarto files.
* https://quarto.org/docs/guide/
* https://quarto.org/docs/reference/
* https://www.jaysong.net/RBook/quarto.html#sec-quarto-howtouse <Japanese>
*
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include "markdown.h"
#include "entry.h"
#include "parse.h"
#include "read.h"
#include <ctype.h>
#include <string.h>
/*
* DATA DEFINITIONS
*/
typedef enum {
K_CHUNK_LABEL = 0,
} quartoKind;
static kindDefinition QuartoKinds[] = {
{ true, 'l', "chunklabel", "chunk labels"},
};
struct sQuartoSubparser {
markdownSubparser markdown;
int lastChunkLabel;
};
/*
* FUNCTION DEFINITIONS
*/
static void findQuartoTags (void)
{
scheduleRunningBaseparser (0);
}
#define skip_space(CP) while (*CP == ' ' || *CP == '\t') CP++;
static int makeQuartoTag (vString *name, int kindIndex, bool anonymous)
{
tagEntryInfo e;
initTagEntry (&e, vStringValue (name), kindIndex);
if (anonymous)
markTagExtraBit (&e, XTAG_ANONYMOUS);
return makeTagEntry (&e);
}
static bool extractLanguageForCodeBlock (markdownSubparser *s,
const char *langMarker,
vString *langName)
{
struct sQuartoSubparser *quarto = (struct sQuartoSubparser *)s;
const char *cp = langMarker;
bool unexecutedBlock = false;
if (*cp != '{')
return false;
cp++;
/* Handle unexecuted blocks like ```{{python}} */
if (*cp == '{') {
unexecutedBlock = true;
cp++;
}
const char *end = strpbrk(cp, " \t,}");
if (!end)
return false;
if (end - cp == 0)
return false;
vStringNCatS (langName, cp, end - cp);
if (unexecutedBlock) {
end = strpbrk(cp, " \t,}");
if (!end)
{
vStringClear (langName);
return false;
}
}
cp = end;
if (*cp == ',' || *cp == '}')
{
vString *name = anonGenerateNew("__anon", K_CHUNK_LABEL);
quarto->lastChunkLabel = makeQuartoTag (name,
K_CHUNK_LABEL,
true);
vStringDelete (name);
return true;
}
skip_space(cp);
vString *chunk_label = vStringNew ();
bool anonymous = false;
while (isalnum((unsigned char)*cp) || *cp == '-')
vStringPut (chunk_label, *cp++);
if (vStringLength (chunk_label) == 0)
{
anonGenerate (chunk_label, "__anon", K_CHUNK_LABEL);
anonymous = true;
}
skip_space(cp);
if (*cp == ',' || *cp == '}')
quarto->lastChunkLabel = makeQuartoTag (chunk_label,
K_CHUNK_LABEL,
anonymous);
vStringDelete (chunk_label);
return true;
}
static void notifyCodeBlockLine (markdownSubparser *s,
const unsigned char *line)
{
struct sQuartoSubparser *quarto = (struct sQuartoSubparser *)s;
if (strncmp ((const char *)line, "#| ", 3))
return;
line += 3;
skip_space (line);
if (strncmp ((const char *)line, "label:", 6))
return;
line += 6;
skip_space (line);
if (!*line)
return;
vString *label = vStringNewInit ((const char *)line);
vStringStripTrailing (label);
if (!vStringIsEmpty (label))
{
/* If an anonymous tag is made for the label of this code chunk,
* it becomes unnecessary; the real one can be made from
* "#! label: ..." */
if (quarto->lastChunkLabel != CORK_NIL)
{
tagEntryInfo *e = getEntryInCorkQueue (quarto->lastChunkLabel);
if (e && isTagExtraBitMarked (e, XTAG_ANONYMOUS))
markTagAsPlaceholder (e, true);
}
quarto->lastChunkLabel = makeQuartoTag (label,
K_CHUNK_LABEL,
false);
}
vStringDelete (label);
}
static void notifyEndOfCodeBlock (markdownSubparser *s)
{
struct sQuartoSubparser *quarto = (struct sQuartoSubparser *)s;
if (quarto->lastChunkLabel == CORK_NIL)
return;
setTagEndLineToCorkEntry (quarto->lastChunkLabel, getInputLineNumber ());
quarto->lastChunkLabel = CORK_NIL;
}
static void inputStart (subparser *s)
{
struct sQuartoSubparser *quatro = (struct sQuartoSubparser*)s;
quatro->lastChunkLabel = CORK_NIL;
}
extern parserDefinition* QuartoParser (void)
{
static const char *const extensions [] = { "qmd", NULL };
static struct sQuartoSubparser quartoSubparser = {
.markdown = {
.subparser = {
.direction = SUBPARSER_SUB_RUNS_BASE,
.inputStart = inputStart,
},
.extractLanguageForCodeBlock = extractLanguageForCodeBlock,
.notifyCodeBlockLine = notifyCodeBlockLine,
.notifyEndOfCodeBlock = notifyEndOfCodeBlock,
},
};
static parserDependency dependencies [] = {
[0] = { DEPTYPE_SUBPARSER, "Markdown", &quartoSubparser },
};
parserDefinition* const def = parserNew ("Quarto");
def->dependencies = dependencies;
def->dependencyCount = ARRAY_SIZE(dependencies);
def->kindTable = QuartoKinds;
def->kindCount = ARRAY_SIZE (QuartoKinds);
def->extensions = extensions;
def->parser = findQuartoTags;
return def;
}