-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlighter.c
77 lines (64 loc) · 1.7 KB
/
highlighter.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
/* PEG Markdown Highlight
* Copyright 2011 Ali Rantakari -- http://hasseg.org
* Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
*
* highlighter.c
*
* Test program that parses the Markdown content from stdin and outputs
* the positions of found language elements.
*/
#include <stdio.h>
#include <string.h>
#include "pmh_parser.h"
#define READ_BUFFER_LEN 1024
char *get_contents(FILE *f)
{
char buffer[READ_BUFFER_LEN];
size_t content_len = 1;
char *content = malloc(sizeof(char) * READ_BUFFER_LEN);
content[0] = '\0';
while (fgets(buffer, READ_BUFFER_LEN, f))
{
content_len += strlen(buffer);
content = realloc(content, content_len);
strcat(content, buffer);
}
return content;
}
void output_result(pmh_element *elem[])
{
pmh_element *cursor;
bool firstType = true;
int i;
for (i = 0; i < pmh_NUM_LANG_TYPES; i++)
{
cursor = elem[i];
if (cursor == NULL)
continue;
if (!firstType)
printf("|");
printf("%i:", i);
bool firstSpan = true;
while (cursor != NULL)
{
if (!firstSpan)
printf(",");
printf("%ld-%ld", cursor->pos, cursor->end);
cursor = cursor->next;
firstSpan = false;
}
firstType = false;
}
}
int main(int argc, char * argv[])
{
pmh_element **result;
FILE *file = stdin;
if (argc > 1)
file = fopen(argv[1], "r");
char *md_source = get_contents(file);
pmh_markdown_to_elements(md_source, pmh_EXT_NONE, &result);
pmh_sort_elements_by_pos(result);
output_result(result);
return(0);
}