-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikiparser.rl
More file actions
1309 lines (1198 loc) · 46.9 KB
/
wikiparser.rl
File metadata and controls
1309 lines (1198 loc) · 46.9 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
C wiki parser library and sample application.
Copyright (c) 2015, Mark Wharton
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//======================================================================
//
// Author: Mark Wharton
//
// Date: 23 December 2015
//
// Title: Wiki Parser
//
// Version: 0.0.9
//
// Topic: Introduction
// **Features**
//
// Supports [[http://www.wikicreole.org/wiki/Creole1.0|Creole 1.0]] with the following limitations:
// * Closing braces cannot be included in preformatted blocks
// * More restrictive use of whitespace
// * No free standing links
// * No image escape character
// * No image inside links
// * No link escape character
// * Space follows list start * and #
// * Unix line breaks (LF) no Windows (CRLF)
//
// **Dependencies**
//
// * [[http://www.complang.org/ragel/|ragel]] for building wikiparser library
//
// **Installation**
//
// Run the configuration script.
// {{{
// ./configure
// }}}
//
// Build the library and programs.
// {{{
// make
// }}}
//
// Install the library and programs.
// {{{
// sudo make install
// }}}
//
// **Using the Wiki Application**
//
// {{{
// Usage: wiki2html [-a] [-b bufferSize] [-m] < input.text > output.html
//
// -a enable additional features specified in the Creole 1.0 Additions
// -m flag monospace in the WIKI_PARSER_TOKEN_TYPE_WIKINOWIKI token
// }}}
//
// **Using the Wiki Library**
//
// {{{
// gcc -I/usr/local/include wikiapp.c -o wikiapp -L/usr/local/lib -lwikiparser
// }}}
//
// **Sample Wiki Application**
//
// {{{
// #include "wikiparser.h"
//
// bool wikiTextWriter(void *userData, WikiParserBuffer *text)
// {
// return fwrite(text->data, text->size, 1, stdout) >= 0;
// }
//
// bool wikiTokenWriter(void *userData, WikiParserToken *token)
// {
// bool error = false;
// if (token->text)
// error = fwrite(token->text->data, token->text->size, 1, stdout) < 0;
// return !error;
// }
//
// int main(int argc, char **argv)
// {
// WikiParser parser = createWikiParser(NULL);
// if (parser) {
// wikiParserConfigureWriters(parser, wikiTextWriter, wikiTokenWriter);
// WikiParserBuffer *buffer = createWikiParserBuffer(WIKI_PARSER_BUFFER_SIZE);
// if (buffer) {
// if (!wikiParserParseStream(parser, buffer, NULL, 0)) {
// fprintf(stderr, "Error: parser error: %d %s (line %d)\n",
// wikiParserGetErrorCode(parser), wikiParserGetErrorString(parser),
// wikiParserGetCurrentLine(parser));
// return 1;
// }
// wikiParserBufferFree(buffer);
// buffer = NULL;
// } else {
// fprintf(stderr, "Error: could not allocate buffer: %lld\n", (long long)buffer->size);
// return 1;
// }
// wikiParserFree(parser);
// parser = NULL;
// } else {
// fprintf(stderr, "Error: could not create parser\n");
// return 1;
// }
// return 0;
// }
// }}}
//
// **Tokens**
//
// The following tokens are defined:
// |= Token Types |= Field |
// | ##WIKI_PARSER_TOKEN_TYPE_NONE [1]## | |
// | ##WIKI_PARSER_TOKEN_TYPE_BOLD## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_CODE## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_DEFINITIONDESC## | |
// | ##WIKI_PARSER_TOKEN_TYPE_DEFINITIONLIST## | |
// | ##WIKI_PARSER_TOKEN_TYPE_DEFINITIONTERM## | |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING0 [2]## | |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING1## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING2## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING3## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING4## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING5## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HEADING6## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_HORIZONTALRULE## | |
// | ##WIKI_PARSER_TOKEN_TYPE_IMAGE [3]## | ##source [text]## |
// | ##WIKI_PARSER_TOKEN_TYPE_ITALIC## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_KEYBOARD## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_LINEBREAK## | |
// | ##WIKI_PARSER_TOKEN_TYPE_LINK [3]## | ##target [text]## |
// | ##WIKI_PARSER_TOKEN_TYPE_LISTITEM## | |
// | ##WIKI_PARSER_TOKEN_TYPE_ORDEREDLIST## | |
// | ##WIKI_PARSER_TOKEN_TYPE_PARAGRAPH## | |
// | ##WIKI_PARSER_TOKEN_TYPE_PREFORMATTED## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_SAMPLECODE## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_SUBSCRIPT## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_SUPERSCRIPT## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_TABLE## | |
// | ##WIKI_PARSER_TOKEN_TYPE_TABLEDATA## | |
// | ##WIKI_PARSER_TOKEN_TYPE_TABLEHEADER## | |
// | ##WIKI_PARSER_TOKEN_TYPE_TABLEROW## | |
// | ##WIKI_PARSER_TOKEN_TYPE_TEXT [4]## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_UNDERLINE## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_UNORDEREDLIST## | |
// | ##WIKI_PARSER_TOKEN_TYPE_VARIABLE## | ##state## |
// | ##WIKI_PARSER_TOKEN_TYPE_WIKINOWIKI [5]## | ##state text## |
// | ##WIKI_PARSER_TOKEN_TYPE_WIKIPLACEHOLDER## | ##text## |
// | ##WIKI_PARSER_TOKEN_TYPE_WIKIPLUGIN## | ##text## |
// **Special Notes:**
// # Sent at the end of the token stream.
// # For calculating header offsets, not used directly.
// # The ##text## value is ##NULL## when image or link does not include pipe.
// # When no text writer is configured (see ##[[#|wikiParserConfigureWriters]]##).
// # The ##monospace## configuration option is passed in the ##state## field for convenience.
//
#include "wikiparser.h"
#include "ragelstuff.h"
#if !defined(NDEBUG)
int g_wikiParserStringCounter = 0;
#endif
#define MARKER_pipe 0
#define MAX_MARKER 1
#define MAX_PSTACK 32
typedef struct WikiParserParserStruct {
void *userData; /* First for wikiParserGetUserData macro. */
WikiParserConfig config; /* Second for special access. */
int blockMode;
int blockModeParam;
int currentLine;
int paragraphCount;
enum WIKI_PARSER_ERROR error;
struct FiniteStateMachineStruct {
int act;
int cs;
const char *ts;
const char *te;
int stack[MAX_PSTACK];
int top;
} fsm;
bool list[5];
const char *marker[MAX_MARKER];
enum WIKI_PARSER_TOKEN_TYPE stack[MAX_PSTACK];
int top;
} WikiParserParser, *WikiParserParserPtr;
#define parserBlockMode (((WikiParserParserPtr)parser)->blockMode)
#define parserBlockModeParam (((WikiParserParserPtr)parser)->blockModeParam)
#define parserConfig (((WikiParserParserPtr)parser)->config)
#define parserCurrentLine (((WikiParserParserPtr)parser)->currentLine)
#define parserError (((WikiParserParserPtr)parser)->error)
#define parserFSM (((WikiParserParserPtr)parser)->fsm)
#define parserList (((WikiParserParserPtr)parser)->list)
#define parserMark(mark) (((WikiParserParserPtr)parser)->marker[MARKER_ ## mark])
#define parserMarker (((WikiParserParserPtr)parser)->marker)
#define parserParagraphCount (((WikiParserParserPtr)parser)->paragraphCount)
#define parserStack (((WikiParserParserPtr)parser)->stack)
#define parserTop (((WikiParserParserPtr)parser)->top)
#define parserUserData (((WikiParserParserPtr)parser)->userData)
#define BLOCKMODE_OUTER 0x00 /* Outer ready mode (emit paragraph tokens when moving to inner paragraph mode). */
#define BLOCKMODE_PMODE 0x01 /* Paragraph mode (do not emit paragraph tokens when moving to inner paragraph mode). */
#define BLOCKMODE_INNER 0x02 /* Inner paragraph mode (emit paragraph tokens when moving from outer ready mode). */
#define BLOCKMODE_DTERM 0x04 /* Definition term. */
#define BLOCKMODE_DDESC 0x08 /* Definition description. */
#define BLOCKMODE_DMASK 0x0C /* Definition term description mask . */
#define BLOCKMODE_LIST1 0x10 /* List level 1. */
#define BLOCKMODE_LIST2 0x20 /* List level 2. */
#define BLOCKMODE_LIST3 0x30 /* List level 3. */
#define BLOCKMODE_LIST4 0x40 /* List level 4. */
#define BLOCKMODE_LIST5 0x50 /* List level 5. */
#define BLOCKMODE_LMASK 0x70 /* List level mask. */
#define BLOCKMODE_SHIFT 4 /* bits to shift for level. */
#define BLOCKMODE_TABLE 0x80 /* Table mode. */
#define BLOCKMODE_CLOSE 0x100 /* Special close flag. */
// private function prototypes
void WikiParserBufferTrim(WikiParserBuffer *text, char character);
bool wikiParserCommitBlock(WikiParser parser);
bool wikiParserCommitFinal(WikiParser parser);
bool wikiParserText(WikiParser parser, WikiParserBuffer *text);
bool wikiParserTextParagraph(WikiParser parser);
bool wikiParserToken(WikiParser parser, WikiParserToken *token);
bool wikiParserTokenClose(WikiParser parser, int tokenType);
bool wikiParserTokenCloseToggles(WikiParser parser);
bool wikiParserTokenOpen(WikiParser parser, int tokenType, int value);
bool wikiParserTokenToggle(WikiParser parser, int tokenType);
bool wikiParserTokenToggle2(WikiParser parser, int topTokenType, int tailTokenType);
// FSM specification
%%{
machine WikiParser;
access parserFSM.;
action additions { parserConfig.additions }
action clear_pipe { parserMark(pipe) = NULL; }
action set_pipe { parserMark(pipe) = fpc; }
newline = '\n' @{ parserCurrentLine += 1; };
any_count_line = ( any | newline );
heading_ = ( '='{1,6} [^\n]+ );
heading = ( heading_ when { parserBlockMode <= BLOCKMODE_PMODE } | newline ' '* heading_ );
horizontal_rule_ = '-'{4,};
horizontal_rule = ( horizontal_rule_ when { parserBlockMode <= BLOCKMODE_PMODE } | newline ' '* horizontal_rule_ );
list_ = ((( '*' | '#' ) ' ' ) | (( '*' | '#' ){2,5} when { parserBlockMode & BLOCKMODE_LMASK }));
list = ( list_ when { parserBlockMode <= BLOCKMODE_PMODE } | newline ' '* list_ );
main := |*
# Wiki creole block level markup.
( heading newline ) => { // headings to level 6
if (!wikiParserCommitBlock(parser)) fbreak;
bool flag = (*parserFSM.ts == '\n');
text.data = parserFSM.ts + (flag ? 1 : 0);
text.size = parserFSM.te - 1 - text.data;
temp = text;
WikiParserBufferTrim(&temp, ' ');
WikiParserBufferTrim(&temp, '=');
int index = temp.data - text.data;
WikiParserBufferTrim(&temp, ' ');
text = temp;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_HEADING0 + index;
token.text = &text;
if (!wikiParserToken(parser, &token)) fbreak;
parserCurrentLine -= 1;
fhold;
};
( horizontal_rule newline ) => { // horizontal rule
if (!wikiParserCommitBlock(parser)) fbreak;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_HORIZONTALRULE;
if (!wikiParserToken(parser, &token)) fbreak;
parserCurrentLine -= 1;
fhold;
};
( list ) => { // lists to level 5
bool flag = (*parserFSM.ts == '\n');
temp.data = parserFSM.ts + (flag ? 1 : 0);
temp.size = parserFSM.te - temp.data;
WikiParserBufferTrim(&temp, ' ');
int index = temp.size;
parserList[index - 1] = (temp.data[temp.size - 1] == '#');
int mode = (parserBlockMode & BLOCKMODE_LMASK) >> BLOCKMODE_SHIFT;
if (mode > index) {
if (!wikiParserTokenCloseToggles(parser)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_LISTITEM)) fbreak;
while (mode > index) {
if (!wikiParserTokenClose(parser, parserList[mode - 1] ? WIKI_PARSER_TOKEN_TYPE_ORDEREDLIST :
WIKI_PARSER_TOKEN_TYPE_UNORDEREDLIST)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_LISTITEM)) fbreak;
mode--;
}
} else if (mode == index) {
if (!wikiParserTokenCloseToggles(parser)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_LISTITEM)) fbreak;
} else {
if ((mode == 0) && !wikiParserCommitBlock(parser)) fbreak;
if (!wikiParserTokenOpen(parser, parserList[index - 1] ? WIKI_PARSER_TOKEN_TYPE_ORDEREDLIST :
WIKI_PARSER_TOKEN_TYPE_UNORDEREDLIST, 0)) fbreak;
}
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_LISTITEM, 0)) fbreak;
parserBlockMode = index << BLOCKMODE_SHIFT;
};
( newline '|' '='? ) => { // table (row & initial cell)
/* Newline is necessary here because without it the token is an exact copy of the next. */
/* This means wiki documents cannot start with table (same for block pre-formatted). */
bool header = (*(parserFSM.te - 1) == '=');
if ((parserBlockMode & BLOCKMODE_TABLE) == 0) {
if (!wikiParserCommitBlock(parser)) fbreak;
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_TABLE, header ? 1 : 0)) fbreak;
} else {
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_TABLEDATA)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_TABLEHEADER)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_TABLEROW)) fbreak;
}
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_TABLEROW, 0)) fbreak;
if (!wikiParserTokenOpen(parser, header ? WIKI_PARSER_TOKEN_TYPE_TABLEHEADER :
WIKI_PARSER_TOKEN_TYPE_TABLEDATA, 0)) fbreak;
parserBlockMode = BLOCKMODE_TABLE;
};
( '|' '='? newline? ) when { parserBlockMode & BLOCKMODE_TABLE } => { // table (cell)
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_TABLEDATA)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_TABLEHEADER)) fbreak;
if (*(parserFSM.te - 1) == '\n') {
parserBlockMode |= BLOCKMODE_CLOSE;
parserCurrentLine -= 1;
fhold;
} else {
bool header = (*(parserFSM.ts + 1) == '=');
if (!wikiParserTokenOpen(parser, header ? WIKI_PARSER_TOKEN_TYPE_TABLEHEADER :
WIKI_PARSER_TOKEN_TYPE_TABLEDATA, 0)) fbreak;
}
};
( newline '{{{' newline any_count_line* :>> ( newline '}}}' )) => { // block pre-formatted
/* Ignoring rule to include closing braces in preformatted blocks. */
/* http://www.wikicreole.org/wiki/Creole1.0#section-Creole1.0-ClosingBracesInNowiki */
if (!wikiParserCommitBlock(parser)) fbreak;
bool flag = (*parserFSM.ts == '\n');
text.data = parserFSM.ts + (flag ? 5 : 4);
text.size = parserFSM.te - 4 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_PREFORMATTED;
token.text = &text;
if (!wikiParserToken(parser, &token)) fbreak;
};
( newline{2,} ) => {
if (!wikiParserCommitBlock(parser)) fbreak;
parserCurrentLine -= 1;
fhold;
};
# Wiki creole inline markup.
( '~'? ( 'http://' | 'ftp://' )) => {
if (!wikiParserTextParagraph(parser)) fbreak;
bool flag = (*parserFSM.ts == '~');
text.data = parserFSM.ts + (flag ? 1 : 0);
text.size = parserFSM.te - parserFSM.ts;
if (!wikiParserText(parser, &text)) fbreak;
};
( '\\\\' ) => { // line break
if (!wikiParserTextParagraph(parser)) fbreak;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_LINEBREAK;
if (!wikiParserToken(parser, &token)) fbreak;
};
( '{{{' any_count_line* :>> '}}}' '}'* ) => { // inline nowiki (optionally rendered in monospace)
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 3;
text.size = parserFSM.te - 3 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_WIKINOWIKI;
token.state = parserConfig.monospace;
token.text = &text;
if (!wikiParserToken(parser, &token)) fbreak;
};
( '<<<' any_count_line* :>> '>>>' '>'* ) => { // placeholder
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 3;
text.size = parserFSM.te - 3 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_WIKIPLACEHOLDER;
token.text = &text;
if (!wikiParserToken(parser, &token)) fbreak;
};
( '<<' any_count_line* :>> '>>' '>'* ) => { // plugin
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 2;
text.size = parserFSM.te - 2 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_WIKIPLUGIN;
token.text = &text;
if (!wikiParserToken(parser, &token)) fbreak;
};
( '[[' @clear_pipe any_count_line* ( '|' @set_pipe any_count_line* )? :>> ']]' ) => { // link
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 2;
text.size = parserFSM.te - 2 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_LINK;
token.target = &temp;
if (parserMark(pipe)) {
temp.data = text.data;
temp.size = parserMark(pipe) - text.data;
text.data = parserMark(pipe) + 1;
text.size = text.size - temp.size - 1;
if (text.size > 4
&& text.data[0] == '{'
&& text.data[1] == '{'
&& text.data[text.size - 2] == '}'
&& text.data[text.size - 1] == '}') {
// image inside links hack
text.data += 2;
text.size -= 4;
token.source = &text;
}
else {
token.text = &text;
}
}
else {
temp.data = text.data;
temp.size = text.size;
}
if (!wikiParserToken(parser, &token)) fbreak;
};
( '{{' @clear_pipe any_count_line* ( '|' @set_pipe any_count_line* )? :>> '}}' ) => { // image (inline)
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 2;
text.size = parserFSM.te - 2 - text.data;
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_IMAGE;
token.source = &temp;
if (parserMark(pipe)) {
temp.data = text.data;
temp.size = parserMark(pipe) - text.data;
text.data = parserMark(pipe) + 1;
text.size = text.size - temp.size - 1;
token.text = &text;
}
else {
temp.data = text.data;
temp.size = text.size;
}
if (!wikiParserToken(parser, &token)) fbreak;
};
( '**' ) => { // bold
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_BOLD)) fbreak;
};
( '//' ) => { // italic
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_ITALIC)) fbreak;
};
( '~' [^\n ] ) => { // escape character
if (!wikiParserTextParagraph(parser)) fbreak;
text.data = parserFSM.ts + 1;
text.size = 1;
if (!wikiParserText(parser, &text)) fbreak;
};
# Wiki creole block level markup additions.
( newline ';' ) when additions => { // definition list (item)
if ((parserBlockMode & BLOCKMODE_DTERM) == 0) {
if (!wikiParserCommitBlock(parser)) fbreak;
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONLIST, 0)) fbreak;
} else {
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONTERM)) fbreak;
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONDESC)) fbreak;
}
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONTERM, 0)) fbreak;
parserBlockMode = BLOCKMODE_DTERM; /* set/reset list */
};
( ':' ) when { parserConfig.additions && ((parserBlockMode & BLOCKMODE_DMASK) == BLOCKMODE_DTERM) } => { // definition list (definition)
if (!wikiParserTokenClose(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONTERM)) fbreak;
if (!wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_DEFINITIONDESC, 0)) fbreak;
parserBlockMode |= BLOCKMODE_DDESC; /* have used up the colon */
};
# Wiki creole inline markup additions.
( '##' ) when additions => { // code
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_CODE)) fbreak;
};
( '#$' ) when additions => { // keyboard head, samplecode tail
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle2(parser,
WIKI_PARSER_TOKEN_TYPE_KEYBOARD, WIKI_PARSER_TOKEN_TYPE_SAMPLECODE)) fbreak;
};
( '$#' ) when additions => { // samplecode head, keyboard tail
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle2(parser,
WIKI_PARSER_TOKEN_TYPE_SAMPLECODE, WIKI_PARSER_TOKEN_TYPE_KEYBOARD)) fbreak;
};
( ',,' ) when additions => { // subscript
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_SUBSCRIPT)) fbreak;
};
( '^^' ) when additions => { // superscript
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_SUPERSCRIPT)) fbreak;
};
( '__' ) when additions => { // underline
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_UNDERLINE)) fbreak;
};
( '$$' ) when additions => { // variable
if (!wikiParserTextParagraph(parser)) fbreak;
if (!wikiParserTokenToggle(parser, WIKI_PARSER_TOKEN_TYPE_VARIABLE)) fbreak;
};
# Paragraph text.
( ( ( alnum+ ) - ( 'http' | 'ftp' ) ) | any_count_line ) => {
text.data = parserFSM.ts;
text.size = parserFSM.te - parserFSM.ts;
bool isNewParagraph = parserBlockMode <= BLOCKMODE_PMODE;
if ((parserBlockMode & BLOCKMODE_CLOSE) && !wikiParserCommitBlock(parser)) fbreak;
if (!wikiParserTextParagraph(parser)) fbreak;
if (parserConfig.blogstyle && !isNewParagraph && *text.data == '\n') {
token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_LINEBREAK;
if (!wikiParserToken(parser, &token)) fbreak;
text.size -= 1;
}
if (text.size) {
if (!wikiParserText(parser, &text)) fbreak;
}
};
*|;
}%%
%% write data;
#pragma unused (WikiParser_en_main)
void wikiParserParserInit(WikiParser parser, int blockMode)
{
parserBlockMode = blockMode & BLOCKMODE_PMODE;
parserBlockModeParam = parserBlockMode;
parserCurrentLine = 1;
parserTop = 0;
RAGEL_WRITE_INIT_PREP(MAX_MARKER);
%% write init;
}
void wikiParserParserExec(WikiParser parser, ExecPrivateBlockData *data)
{
WikiParserBuffer temp = emptyWikiParserBuffer;
WikiParserBuffer text = emptyWikiParserBuffer;
WikiParserToken token = emptyWikiParserToken;
RAGEL_WRITE_EXEC_IN();
%% write exec;
RAGEL_WRITE_EXEC_OUT();
}
// private functions
void WikiParserBufferTrim(WikiParserBuffer *text, char character)
{
const char *trim = text->data + text->size;
while (text->data < trim && *text->data == character) text->data++;
while (trim > text->data && *(trim - 1) == character) trim--;
text->size = trim - text->data;
}
bool wikiParserCommitBlock(WikiParser parser)
{
bool error = false;
int index = parserTop;
while (index) {
int tokenType = parserStack[index - 1];
WikiParserToken token = emptyWikiParserToken;
token.type = tokenType;
token.state = false;
error = !wikiParserToken(parser, &token);
if (error) break;
index--;
};
parserBlockMode = parserBlockModeParam;
parserTop = 0;
return !error;
}
bool wikiParserCommitFinal(WikiParser parser)
{
bool error = !wikiParserCommitBlock(parser);
if (!error) {
WikiParserToken token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_NONE;
error = !wikiParserToken(parser, &token);
}
return !error;
}
bool wikiParserText(WikiParser parser, WikiParserBuffer *text)
{
bool error = false;
if (parserConfig.writeText) {
if (!parserConfig.writeText(parserUserData, text)) {
parserError = WIKI_PARSER_ERROR_WRITER;
error = true;
}
} else {
WikiParserToken token = emptyWikiParserToken;
token.type = WIKI_PARSER_TOKEN_TYPE_TEXT;
token.text = text;
error = !wikiParserToken(parser, &token);
}
return !error;
}
bool wikiParserTextParagraph(WikiParser parser)
{
bool error = false;
if (parserBlockMode <= BLOCKMODE_PMODE) {
if (parserBlockMode == BLOCKMODE_OUTER) {
error = !wikiParserTokenOpen(parser, WIKI_PARSER_TOKEN_TYPE_PARAGRAPH, ++(parserParagraphCount));
}
parserBlockMode = BLOCKMODE_INNER;
}
return !error;
}
bool wikiParserToken(WikiParser parser, WikiParserToken *token)
{
bool error = false;
if (parserConfig.writeToken) {
if (!parserConfig.writeToken(parserUserData, token)) {
parserError = WIKI_PARSER_ERROR_WRITER;
error = true;
}
}
return !error;
}
bool wikiParserTokenClose(WikiParser parser, int tokenType)
{
bool error = false;
int index = parserTop;
while (index) {
if (parserStack[index - 1] == tokenType) break;
index--;
};
if (index > 0) {
WikiParserToken token = emptyWikiParserToken;
token.type = tokenType;
token.state = false;
error = !wikiParserToken(parser, &token);
while (index < parserTop) {
parserStack[index - 1] = parserStack[index];
index++;
}
parserTop--;
}
return !error;
}
bool wikiParserTokenCloseToggles(WikiParser parser)
{
bool error = false;
int index = parserTop;
while (index && !error) {
int tokenType = parserStack[index - 1];
switch (tokenType) {
case WIKI_PARSER_TOKEN_TYPE_BOLD:
case WIKI_PARSER_TOKEN_TYPE_CODE:
case WIKI_PARSER_TOKEN_TYPE_ITALIC:
case WIKI_PARSER_TOKEN_TYPE_KEYBOARD:
case WIKI_PARSER_TOKEN_TYPE_SAMPLECODE:
case WIKI_PARSER_TOKEN_TYPE_SUBSCRIPT:
case WIKI_PARSER_TOKEN_TYPE_SUPERSCRIPT:
case WIKI_PARSER_TOKEN_TYPE_UNDERLINE:
case WIKI_PARSER_TOKEN_TYPE_VARIABLE:
error = !wikiParserTokenClose(parser, tokenType);
break;
}
index--;
};
return !error;
}
bool wikiParserTokenOpen(WikiParser parser, int tokenType, int value)
{
bool error = false;
WikiParserToken token = emptyWikiParserToken;
token.type = tokenType;
token.state = true;
token.value = value;
error = !wikiParserToken(parser, &token);
parserStack[parserTop++] = tokenType;
return !error;
}
bool wikiParserTokenToggle(WikiParser parser, int tokenType)
{
return wikiParserTokenToggle2(parser, tokenType, tokenType);
}
bool wikiParserTokenToggle2(WikiParser parser, int topTokenType, int tailTokenType)
{
bool error = false;
int index = parserTop;
while (index) {
if (parserStack[index - 1] == tailTokenType) break;
index--;
};
if (index == 0) {
int tokenType = parserStack[parserTop++] = topTokenType;
WikiParserToken token = emptyWikiParserToken;
token.type = tokenType;
token.state = true;
error = !wikiParserToken(parser, &token);
} else {
WikiParserToken token = emptyWikiParserToken;
token.type = tailTokenType;
token.state = false;
error = !wikiParserToken(parser, &token);
while (index < parserTop) {
parserStack[index - 1] = parserStack[index];
index++;
}
parserTop--;
}
return !error;
}
// public functions
//======================================================================
//
// Spec: Wiki Parser API
//
//----------------------------------------------------------------------
//
// Func: createWikiParser => WikiParser WIKIPARSERAPI createWikiParser(WikiParserConfig *config)
// Create a new ##parser## object. The parser must be freed with ##[[#|wikiParserFree]]## when no longer needed.
//
// Param: config => WikiParserConfig *
// (optional) The config object. If the value is NULL the standard ##emptyWikiParserConfig## config is used.
// With ##emptyWikiParserConfig## all options are set to ##false## and the writers set to ##NULL##.
//
// Return: parser => WikiParser
// The new parser object. Note: The value will be ##NULL## if memory could not be allocated.
//
WikiParser WIKIPARSERAPI createWikiParser(WikiParserConfig *config)
{
WikiParser parser = malloc(sizeof(WikiParserParser));
if (parser) {
parserUserData = NULL;
parserConfig = config ? *config : emptyWikiParserConfig;
parserCurrentLine = 0;
parserError = 0;
parserParagraphCount = 0;
}
return parser;
}
//----------------------------------------------------------------------
//
// Func: createWikiParserBuffer => WikiParserBuffer WIKIPARSERAPI *createWikiParserBuffer(size_t size)
// Create a new ##buffer## object. The buffer must be freed with ##[[#|wikiParserBufferFree]]## when no longer needed.
//
// Param: size => size_t
// The buffer size.
//
// Return: buffer => WikiParserBuffer *
// The new buffer object. Note: The value will be ##NULL## if memory could not be allocated.
//
WikiParserBuffer WIKIPARSERAPI *createWikiParserBuffer(size_t size)
{
if (size <= 0) size = WIKI_PARSER_BUFFER_SIZE;
WikiParserBuffer *buffer = malloc(sizeof(WikiParserBufferPtr) + size + 1);
if (buffer) {
buffer->data = (const char *)(buffer + sizeof(WikiParserBufferPtr));
((char *)buffer->data)[size] = '\0';
buffer->size = size;
}
return buffer;
}
//----------------------------------------------------------------------
//
// Func: createWikiParserString => char WIKIPARSERAPI *createWikiParserString(const char *data, size_t size)
// Create a new ##string## object with text. The string must be freed with ##[[#|wikiParserStringFree]]## when no longer needed.
//
// Param: data => const char *
// The text buffer data.
//
// Param: size => size_t
// The text buffer size.
//
// Return: string => char *
// The new string object. Note: The value will be ##NULL## if memory could not be allocated.
//
char WIKIPARSERAPI *createWikiParserString(const char *data, size_t size)
{
assert(data);
assert(size >= 0);
char *string = malloc(size + 1);
if (string) {
#if !defined(NDEBUG)
g_wikiParserStringCounter++;
#endif
strncpy(string, data, size);
string[size] = '\0';
}
return string;
}
//----------------------------------------------------------------------
//
// Func: wikiParserBufferFree => void WIKIPARSERAPI wikiParserBufferFree(WikiParserBuffer *buffer)
// Free the memory allocated to ##buffer##. The buffer must be a valid buffer created with an earlier
// call to [[#|createWikiParserBuffer]]. The buffer value must not be used after it is freed. It
// is good practice to set the buffer value to ##NULL## after calling this function.
//
// Param: buffer => WikiParserBuffer *
// The buffer to be freed.
//
// Return: none => void
//
void WIKIPARSERAPI wikiParserBufferFree(WikiParserBuffer *buffer)
{
assert(buffer);
free(buffer);
}
//----------------------------------------------------------------------
//
// Func: wikiParserConfigureOptions => void WIKIPARSERAPI wikiParserConfigureOptions(WikiParser parser, bool additions, bool blogstyle, bool monospace)
// Configure the options for the parser. These options are applied to a copy of the original config object (if any)
// which was passed in the call to [[#|createWikiParser]]. The original config object remains unchanged.
//
// Param: parser => WikiParser
// The parser object.
//
// Param: additions => bool
// ##true## to enable additional features specified in the [[http://www.wikicreole.org/wiki/CreoleAdditions|Creole 1.0 Additions]], including:
// * Code (Monospace)
// * Definition lists
// * Plug-in/Extension
// * Subscript
// * Superscript
// * Underline
//
// and experimental features, including:
// * (##{{{#$...$#}}}##) Keyboard
// * (##{{{$#...#$}}}##) Samplecode
// * (##{{{$$...$$}}}##) Variable
//
// Param: blogstyle => bool
// ##true## to enable blog-style line breaks instead of wiki-style line breaks (the default). See
// [[http://www.wikicreole.org/wiki/ParagraphsAndLineBreaksReasoning|Creole 1.0 Paragraphs And Line Breaks Reasoning]]
// for details.
//
// Param: monospace => bool
// ##true## to enable monospace in the ##WIKI_PARSER_TOKEN_TYPE_WIKINOWIKI## token. This option is passed to the token writer, the parser does not do anything special.
// See [[http://www.wikicreole.org/wiki/Creole1.0#section-Creole1.0-NowikiPreformatted|Creole 1.0 Nowiki Preformatted]] for more information.
//
// Return: none => void
//
void WIKIPARSERAPI wikiParserConfigureOptions(WikiParser parser, bool additions, bool blogstyle, bool monospace)
{
assert(parser);
parserConfig.additions = additions;
parserConfig.blogstyle = blogstyle;
parserConfig.monospace = monospace;
}
//----------------------------------------------------------------------
//
// Func: wikiParserConfigureWriters => void WIKIPARSERAPI wikiParserConfigureWriters(WikiParser parser, WriteTextFunc writeText, WriteTokenFunc writeToken)
// Configure the text and token writer callbacks for the parser. These writer callbacks are applied to a copy of the original config
// object (if any) which was passed in the call to [[#|createWikiParser]]. The original config object remains unchanged.
//
// The ##WriteTextFunc## text and ##WriteTokenFunc## token writer callbacks are declared as follows:
//
// {{{
// typedef bool (*WriteTextFunc)(void *userData, WikiParserBuffer *text);
//
// bool wikiTextWriter(void *userData, WikiParserBuffer *text)
// {
// bool error = false;
// // process the text...
// return !error;
// }
//
// typedef bool (*WriteTokenFunc)(void *userData, WikiParserToken *token);
//
// bool wikiTokenWriter(void *userData, WikiParserToken *token)
// {
// bool error = false;
// // process the token...
// return !error;
// }
// }}}
//
// Return ##true## on success and ##false## on failure. Record any specific error details in ##userData## if they are needed.
//
// Param: parser => WikiParser
// The parser object.
//
// Param: writeText => WriteTextFunc
// (optional) The text writer callback. If the value is ##NULL## text is sent to the
// token writer in the ##WIKI_PARSER_TOKEN_TYPE_TEXT## token.
//
// Param: writeToken => WriteTokenFunc
// The token writer callback.
//
// Return: none => void
//
void WIKIPARSERAPI wikiParserConfigureWriters(WikiParser parser, WriteTextFunc writeText, WriteTokenFunc writeToken)
{
assert(parser);
parserConfig.writeText = writeText;
parserConfig.writeToken = writeToken;
}
//----------------------------------------------------------------------
//
// Func: wikiParserFree => void WIKIPARSERAPI wikiParserFree(WikiParser parser)
// Free the memory allocated to ##parser##. The parser must be a valid parser created with an earlier
// call to [[#|createWikiParser]]. The parser value must not be used after it is freed. It
// is good practice to set the parser value to ##NULL## after calling this function.
//
// Param: parser => WikiParser
// The parser to be freed.
//
// Return: none => void
//
void WIKIPARSERAPI wikiParserFree(WikiParser parser)
{
assert(parser);
free(parser);
}
//----------------------------------------------------------------------
//
// Func: wikiParserGetCurrentLine => int WIKIPARSERAPI wikiParserGetCurrentLine(WikiParser parser)
// Return the current line of the parser. Line numbers start from ##1## and continue to the end of the document.
// Before the parser has started the value will be ##0##, on completion the value will be the total number of