-
Notifications
You must be signed in to change notification settings - Fork 69
/
markdown_lib.m
163 lines (138 loc) · 7.16 KB
/
markdown_lib.m
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
/**********************************************************************
markdown_lib.m - markdown in Cocoa using a PEG grammar.
(c) 2012 Gregory Wieber & Jim Radford
(c) 2011 David Whetstone (david at humblehacker dot com).
(c) 2008 John MacFarlane (jgm at berkeley dot edu).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License or the MIT
license. See LICENSE for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#import "markdown_peg.h"
#define TABSTOP 4
/* preformat_text - allocate and copy text buffer while
* performing tab expansion. */
static NSMutableString *preformat_text(NSString *text) {
NSMutableString *buf = [[[NSMutableString alloc] init] autorelease];
unichar next_char;
int len = 0;
int charstotab = TABSTOP;
for (NSUInteger i = 0; i < text.length; ++i) {
next_char = [text characterAtIndex:i];
switch (next_char) {
case '\t':
while (charstotab > 0)
[buf appendCharacter:' '], len++, charstotab--;
break;
case '\n':
[buf appendCharacter:'\n'], len++, charstotab = TABSTOP;
break;
default:
[buf appendCharacter:next_char], len++, charstotab--;
}
if (charstotab == 0)
charstotab = TABSTOP;
}
[buf appendString:@"\n\n"];
return(buf);
}
/* process_raw_blocks - traverses an element list, replacing any RAW elements with
* the result of parsing them as markdown text, and recursing into the children
* of parent elements. The result should be a tree of elements without any RAWs. */
static element * process_raw_blocks(element *input, int extensions, element *references, element *notes) {
element *current = NULL;
element *last_child = NULL;
current = input;
while (current != NULL) {
if (current->key == RAW) {
current->key = LIST;
/* \001 is used to indicate boundaries between nested lists when there
* is no blank line. We split the string by \001 and parse
* each chunk separately. */
NSArray *chunks = [current->contents.str componentsSeparatedByString:@"\001"];
for (NSString *contents in chunks) {
if (!last_child) {
current->children = parse_markdown(contents, extensions, references, notes);
last_child = current->children;
} else {
while (last_child->next != NULL)
last_child = last_child->next;
last_child->next = parse_markdown(contents, extensions, references, notes);
}
}
[current->contents.str release];
current->contents.str = nil;
}
if (current->children != NULL)
current->children = process_raw_blocks(current->children, extensions, references, notes);
current = current->next;
}
return input;
}
NSMutableAttributedString* markdown_to_attr_string(NSString *text, int extensions, NSDictionary* attributes) {
NSMutableAttributedString *out = [[[NSMutableAttributedString alloc] init] autorelease];
NSMutableString *formatted_text = preformat_text(text);
element *references = parse_references(formatted_text, extensions);
element *notes = parse_notes(formatted_text, extensions, references);
element *result = parse_markdown(formatted_text, extensions, references, notes);
result = process_raw_blocks(result, extensions, references, notes);
[out beginEditing];
NSDictionary *_attributes[] = {
[LIST] = [attributes objectForKey:[NSNumber numberWithInt:LIST]],
[RAW] = [attributes objectForKey:[NSNumber numberWithInt:RAW]],
[SPACE]= [attributes objectForKey:[NSNumber numberWithInt:SPACE]],
[LINEBREAK]= [attributes objectForKey:[NSNumber numberWithInt:LINEBREAK]],
[ELLIPSIS]= [attributes objectForKey:[NSNumber numberWithInt:ELLIPSIS]],
[EMDASH]= [attributes objectForKey:[NSNumber numberWithInt:EMDASH]],
[ENDASH]= [attributes objectForKey:[NSNumber numberWithInt:ENDASH]],
[APOSTROPHE]= [attributes objectForKey:[NSNumber numberWithInt:APOSTROPHE]],
[SINGLEQUOTED]= [attributes objectForKey:[NSNumber numberWithInt:SINGLEQUOTED]],
[DOUBLEQUOTED]= [attributes objectForKey:[NSNumber numberWithInt:DOUBLEQUOTED]],
[STRING]= [attributes objectForKey:[NSNumber numberWithInt:STRING]],
[LINK]= [attributes objectForKey:[NSNumber numberWithInt:LINK]],
[IMAGE]= [attributes objectForKey:[NSNumber numberWithInt:IMAGE]],
[CODE]= [attributes objectForKey:[NSNumber numberWithInt:CODE]],
[HTML]= [attributes objectForKey:[NSNumber numberWithInt:HTML]],
[EMPH]= [attributes objectForKey:[NSNumber numberWithInt:EMPH]],
[STRONG]= [attributes objectForKey:[NSNumber numberWithInt:STRONG]],
[PLAIN]= [attributes objectForKey:[NSNumber numberWithInt:PLAIN]],
[PARA]= [attributes objectForKey:[NSNumber numberWithInt:PARA]],
[LISTITEM]= [attributes objectForKey:[NSNumber numberWithInt:LISTITEM]],
[BULLETLIST]= [attributes objectForKey:[NSNumber numberWithInt:BULLETLIST]],
[ORDEREDLIST]= [attributes objectForKey:[NSNumber numberWithInt:ORDEREDLIST]],
[H1]= [attributes objectForKey:[NSNumber numberWithInt:H1]],
[H2]= [attributes objectForKey:[NSNumber numberWithInt:H2]],
[H3]= [attributes objectForKey:[NSNumber numberWithInt:H3]],
[H4]= [attributes objectForKey:[NSNumber numberWithInt:H4]],
[H5]= [attributes objectForKey:[NSNumber numberWithInt:H5]],
[H6]= [attributes objectForKey:[NSNumber numberWithInt:H6]],
[BLOCKQUOTE]= [attributes objectForKey:[NSNumber numberWithInt:BLOCKQUOTE]],
[VERBATIM]= [attributes objectForKey:[NSNumber numberWithInt:VERBATIM]],
[HTMLBLOCK]= [attributes objectForKey:[NSNumber numberWithInt:HTMLBLOCK]],
[HRULE]= [attributes objectForKey:[NSNumber numberWithInt:HRULE]],
[REFERENCE]= [attributes objectForKey:[NSNumber numberWithInt:REFERENCE]],
[NOTE]= [attributes objectForKey:[NSNumber numberWithInt:NOTE]],
};
print_element_list_attr(out, result, extensions, _attributes, @{});
[out endEditing];
free_element_list(result);
free_element_list(references);
return out;
}
@implementation NSString (Sugar)
- (const char *)defaultCString {
return [self cStringUsingEncoding:[NSString defaultCStringEncoding]];
}
@end
@implementation NSMutableString (Sugar)
- (void)appendCharacter:(unichar)ch {
[self appendFormat:@"%C", ch];
}
@end
/* vim:set ts=4 sw=4: */