forked from zaniar/md2roff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2roff.c
1447 lines (1352 loc) · 32.8 KB
/
md2roff.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
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
/*
* md2roff.c
* A utility to convert markdown documents to troff.
*
* Copyright (C) 2017, Nicholas Christopoulos (mailto:[email protected])
*
* License GPL3+
* CC: std C99
* URL: http://github.com/nereusx/md2roff
*
* history:
* 2017-05-08, created
* 2019-02-10, cleanup
* 2021-02-13, SYNTAX and a few improvements for man-pages
* 2021-03-09, v1.4
* 2021-06-26, v1.5, ms package
* 2021-09-18, v1.6, option --synopsis-style
* 2022-06-26, v1.7, regex added to support -z better
* 2022-07-13, v1.8, blockquotes
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License.
* See LICENSE for details.
*/
#include <stdbool.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <regex.h>
// options
typedef enum { mp_mm, mp_man, mp_mdoc, mp_mom, mp_ms } macropackage_t;
macropackage_t mpack = mp_man;
int man_ofc = 0, write_lock = 0, std_q = 1;
int opt_name_style = 0;
typedef struct { const char *wrong, *correct; } dict_line_t;
dict_line_t mdic[] = {
{ "bitmask", "bit mask" },
{ "builtin", "build-in" },
{ "epoch", "Epoch" },
{ "file name", "filename" },
{ "file system", "filesystem" },
{ "host name", "hostname" },
{ "i-node", "inode" },
{ "i-nodes", "inodes" },
{ "lower case", "lowercase" },
{ "lower-case", "lowercase" },
{ "upper case", "uppercase" },
{ "upper-case", "uppercase" },
{ "path name", "pathname" },
{ "pseudo-terminal", "pseudoterminal" },
{ "real time", "real-time" },
{ "realtime", "real-time" },
{ "runtime", "run time" },
{ "super user", "superuser" },
{ "super-user", "superuser" },
{ "super block", "superblock" },
{ "super-block", "superblock" },
{ "time stamp", "timestamp" },
{ "time zone", "timezone" },
{ "userspace", "user space" },
{ "user name", "username" },
{ "x86_64", "x86-64" },
{ "zeroes", "zeros" }, // ?!
{ "32bit", "32-bit" },
{ "Unices", "Unix systems" },
{ "Unixes", "Unix systems" },
{ "man page", "manual page" },
{ "man pages", "manual pages" },
{ "manpage", "manual page" },
{ "manpages", "manual pages" },
{ "minus infinity", "negative infinity" },
{ "non-root", "unprivileged user" },
{ "non-superuser", "unprivileged user" },
{ "nonprivileged", "unprivileged" },
{ NULL, NULL } };
/*
* if 'when' is true, print error message and quit
*/
void panicif(int when, const char *fmt, ...) {
char msg[1024];
va_list ap;
va_start(ap, fmt);
if ( when ) {
vsnprintf(msg, 1024, fmt, ap);
fprintf(stderr, "%s [%s]\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
va_end(ap);
}
/*
* clone string
*/
char *strdup(const char *s) {
char *d = (char *) malloc(strlen(s) + 1);
if ( d )
strcpy(d, s);
return d;
}
/*
* squeeze (& strdup)
*/
char *sqzdup(const char *source) {
char *rp, *p, *d;
int lc = 0;
rp = malloc(strlen(source) + 1);
p = (char *) source;
d = rp;
while ( isspace(*p) ) p ++;
while ( *p ) {
if ( isspace(*p) ) {
if ( !lc ) {
lc = 1;
if ( p > source ) {
if ( isalnum(*(p-1)) || strchr(",;.)}]", *(p-1)) )
*d ++ = ' ';
else {
char *nc = p;
while ( isspace(*nc) )
nc ++;
if ( isalnum(*nc) )
*d ++ = ' ';
}
}
}
}
else {
lc = 0;
*d ++ = *p;
}
p ++;
}
*d = '\0';
if ( d > rp ) {
if ( isspace(*(d - 1)) )
*(d - 1) = '\0';
}
return rp;
}
/*
* regex find & replace
*/
char* regex_find_and_replace(const char *src, regex_t *re, const char *rp) {
size_t size = 0x10000 + strlen(src);
char *buf = (char *) malloc(size);
char *pos;
int sub, so, n;
regmatch_t pmatch[10]; /* regoff_t is int so size is int */
strcpy(buf, src);
if ( regexec(re, buf, 10, pmatch, 0) != REG_NOMATCH ) {
// first do preliminary replacements in the replacement text i.e. \1 -> first match
for ( pos = (char *) rp; *pos; pos ++ ) {
if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
so = pmatch [*(pos + 1) - 48].rm_so;
n = pmatch [*(pos + 1) - 48].rm_eo - so;
if ( so < 0 || strlen(rp) + n - 1 > size ) return buf;
memmove(pos + n, pos + 2, strlen(pos) - 1);
memmove(pos, buf + so, n);
pos = pos + n - 2;
}
}
sub = pmatch [1].rm_so; // repeated replace when sub >= 0
for ( pos = buf; !regexec(re, pos, 1, pmatch, 0); ) {
n = pmatch [0].rm_eo - pmatch [0].rm_so;
pos += pmatch [0].rm_so;
if ( strlen(buf) - n + strlen(rp) + 1 > size )
break;
memmove(pos + strlen(rp), pos + n, strlen(pos) - n + 1);
memmove(pos, rp, strlen (rp));
pos += strlen(rp);
if ( sub >= 0 ) break;
}
}
return realloc(buf, strlen(buf) + 1);
}
/*
* Loads the `filename` file into memory and return a pointer to its contents.
* The pointer must freed by the user.
*/
char *loadfile(const char *filename) {
int len = -1, c, alloc;
FILE *fp;
char *buf;
if ( filename == NULL ) {
len = 0;
alloc = 1024;
buf = (char *) malloc(alloc);
while ( (c = fgetc(stdin)) != EOF ) {
buf[len] = c;
len ++;
if ( len >= alloc ) {
alloc += 1024;
buf = (char *) realloc(buf, alloc);
}
}
}
else {
panicif((fp = fopen(filename, "r")) == NULL, "Unable to open '%s'", filename);
panicif((fseek(fp, 0L, SEEK_END) == -1), "fseek failed");
panicif((len = ftell(fp)) == -1, "ftell failed");
panicif((fseek(fp, 0L, SEEK_SET) == -1), "fseek failed");
buf = (char *) malloc(len+1);
panicif((fread(buf, len, 1, fp) == -1), "fread failed");
buf[len] = '\0';
fclose(fp);
if ( man_ofc ) {
char pat[256], *np;
regex_t regex;
int reti;
for ( int i = 0; mdic[i].wrong; i ++ ) {
strcpy(pat, mdic[i].wrong);
reti = regcomp(®ex, pat, REG_EXTENDED | REG_ICASE);
if ( reti == 0 ) { // compilation passed
np = regex_find_and_replace(buf, ®ex, mdic[i].correct);
free(buf);
buf = np;
regfree(®ex);
}
}
}
}
return buf;
}
/*
* adds the string 'str' to buffer 'buf' and returns
* a pointer to new position in buf.
*/
char *stradd(char *buf, const char *str) {
const char *p = str;
char *d = buf;
while ( *p )
*d ++ = *p ++;
return d;
}
/*
* prints the whole line of 'src' and returns pointer
* to the next character (the first of the next line).
*/
const char *println(const char *src) {
const char *p = src;
while ( *p ) {
if ( !write_lock )
putchar(*p);
p ++;
if ( *(p-1) == '\n' )
break;
}
return p;
}
/*
* types of elements
*/
enum { none,
par_end, ln_brk,
cblock_end, cblock_open,
li_open, li_end,
ol_open, ul_open, lst_close,
man_ref, ol, ul,
bq_open, bq_close,
box_open, box_close,
url_mark,
tbl_open, tbl_close,
new_sh, new_ss, new_s4 };
/*
* list (enumeration/itemize) stack
*/
#define MAX_LIST_SIZE 32
int stk_list[MAX_LIST_SIZE]; // type of list
int stk_count[MAX_LIST_SIZE]; // counter of item
int stk_list_p = 0; // top pointer, always points to first free
int bq_level = 0, prev_bq_level = 0;
/*
* write the roff code of 'type'
*/
void roff(int type, ...) {
va_list ap;
char *title, *link;
char punc;
if ( write_lock ) {
va_start(ap, type);
va_end(ap);
return;
}
va_start(ap, type);
//
if ( bq_level != prev_bq_level ) {
int i;
if ( bq_level < prev_bq_level ) {
for ( i = bq_level; i < prev_bq_level; i ++ )
puts(".RE");
}
else {
for ( i = prev_bq_level; i < bq_level; i ++ )
puts(".RS");
}
prev_bq_level = bq_level;
}
//
switch ( type ) {
case none:
break;
// new paragraph
case par_end:
switch ( mpack ) {
case mp_mdoc: puts(".Pp"); break;
default: puts(".PP");
}
break;
// line break
case ln_brk:
switch ( mpack ) {
case mp_mom: puts(".BR"); break; // or .br or .EL or .LINEBREAK ????
case mp_ms: puts(".BR"); break;
default: puts(".br");
}
break;
// link
case url_mark:
title = va_arg(ap, char *);
link = va_arg(ap, char *);
punc = va_arg(ap, int);
switch ( mpack ) {
case mp_man:
if ( strchr(link, '@') ) {
if ( strlen(title) && strcmp(title, link) != 0 )
printf(".MT %s\n%s\n", link, title);
else
printf(".MT %s\n", link);
if ( punc )
printf(".ME %c\n", punc);
else
printf(".ME\n");
}
else {
if ( strlen(title) && strcmp(title, link) != 0 )
printf(".UR %s\n%s\n", link, title);
else
printf(".UR %s\n", link);
if ( punc )
printf(".UE %c\n", punc);
else
printf(".UE\n");
}
break;
case mp_mdoc:
if ( strchr(link, '@') )
printf(".An %s Aq Mt %s\n", title, link);
else
printf(".Lk %s \"%s\"\n", link, title);
break;
case mp_mm: // there is no such thing...
case mp_ms:
printf("%s <%s>\n", title, link);
break;
case mp_mom:
printf("%s \\*[UL]%s\\*[ULX]\n", title, link);
}
break;
// cartouche top
case box_open:
switch ( mpack ) {
case mp_mom: puts(".DRH"); break;
case mp_man: puts(".B"); break;
case mp_ms: puts(".B1"); break;
default: puts(".FT B");
}
break;
// cartouche bottom
case box_close:
switch ( mpack ) {
case mp_mom: puts(".DRH"); break;
case mp_ms: puts(".B2"); break;
default: puts(".FT P");
}
break;
// code block - begin
case cblock_open:
switch ( mpack ) {
case mp_mom: printf(".CODE\n"); break;
case mp_mdoc: printf(".Bd -literal -offset indent\n"); break;
case mp_ms: puts(".DS I"); break;
default: printf(".in +4n\n.EX\n");
}
break;
// code block - end
case cblock_end:
switch ( mpack ) {
case mp_mom: printf(".CODE OFF\n"); break;
case mp_mdoc: printf(".Ed\n"); break;
case mp_ms: puts(".DE"); break;
default: printf(".EE\n.in\n");
}
break;
// ordered list (1..2..3..)
case ol_open:
stk_list[stk_list_p] = ol;
stk_count[stk_list_p] = 1;
stk_list_p ++;
switch ( mpack ) {
case mp_mom:
switch ( stk_list_p ) {
case 1: puts(".LIST DIGIT"); break;
case 2: puts(".LIST ALPHA"); break;
case 3: puts(".LIST DIGIT"); break;
case 4: puts(".LIST alpha"); break;
default:
puts(".LIST DIGIT");
};
break;
case mp_mdoc: puts(".Bl -enum -offset indent"); break;
case mp_mm: puts(".AL"); break;
}
break;
// unordered list (bullets)
case ul_open:
stk_list[stk_list_p] = ul;
stk_count[stk_list_p] = 1;
stk_list_p ++;
switch ( mpack ) {
case mp_mom:
printf(".LIST %s", ((stk_list_p % 2) ? "BULLET" : "DASH"));
break;
case mp_mdoc:
printf(".Bl -%s -offset indent", ((stk_list_p % 2) ? "bullet" : "dash"));
break;
case mp_mm: puts(".BL");
}
break;
// close list
case lst_close:
switch ( mpack ) {
case mp_mom: puts(".LIST OFF"); break;
case mp_mdoc: puts(".El");
}
break;
// list item - begin
case li_open:
switch ( mpack ) {
case mp_mom: puts(".ITEM"); break;
case mp_mdoc: puts(".It"); break;
case mp_man:
case mp_ms:
if ( stk_list_p ) {
if ( stk_list[stk_list_p-1] == ul )
puts(".IP \\(bu 4");
else {
printf(".IP %d. 4\n", stk_count[stk_list_p-1]);
stk_count[stk_list_p-1] ++;
}
}
break;
default: puts(".LI");
}
break;
// list item - end
case li_end:
if ( mpack == mp_mm ) puts(".LE");
break;
// new big header/section
case new_sh:
switch ( mpack ) {
case mp_mom: printf(".HEADING 1 \""); break;
case mp_mdoc: printf(".Sh "); break;
case mp_ms: puts(".SH "); break; /* .SH\n...\n.LP|.PP\n */
default: printf(".SH ");
}
break;
// new medium header/secrtion
case new_ss:
switch ( mpack ) {
case mp_mom: printf(".HEADING 2 \""); break;
case mp_mdoc: printf(".Ss "); break;
case mp_ms: puts(".SH "); break;
default: printf(".SS ");
}
break;
// new small header/secrtion
case new_s4:
switch ( mpack ) {
case mp_mom: printf(".HEADING 3 \""); break;
case mp_ms: puts(".SH "); break;
case mp_mdoc: printf(".Ss "); break;
default: printf(".SS ");
}
break;
// table
case tbl_open:
switch ( mpack ) {
default: printf(".TS\ntab(|);\n.\n");
}
break;
case tbl_close:
switch ( mpack ) {
default: printf(".TE\n");
}
break;
// reference to man page
case man_ref:
link = va_arg(ap, char *);
punc = va_arg(ap, int);
switch ( mpack ) {
case mp_mdoc: printf(".Xr %s\n", link); break;
case mp_man: {
char *tmp = strdup(link);
char *p = strchr(tmp, ' ');
if ( p ) {
*p = '\0';
printf(".BR %s (%s)", tmp, p+1);
}
else
printf(".BR %s", link);
free(tmp);
if ( punc )
printf("%c\n", punc);
else
printf("\n");
}
break;
default: printf("%s\n", link);
}
break;
}
va_end(ap);
}
/*
* write buffer and reset
*/
char *flushln(char *d, char *bf) {
if ( d > bf ) {
*d = '\0';
d = bf;
while ( isspace(*d) )
d ++;
if ( *d ) {
char *z = sqzdup(d);
if ( !write_lock ) puts(z);
free(z);
}
}
return bf;
}
/*
* this converts the file 'docname',
* that is loaded in 'source', to *-roff.
*/
#define KEY_GNUSYN "SYNTAX:"
#define KEY_NDCCMD "COMMAND:"
static char *month[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
NULL };
//
#define MAX_STR 255
const char *get_man_header(const char *source, char *name, char *section, char *date) {
const char *p = source;
char *d;
while ( isblank(*p) ) p ++;
d = name;
while ( *p ) {
if ( isspace(*p) ) break;
if ( d - name >= MAX_STR ) break;
*d ++ = toupper(*p ++);
}
*d = '\0';
while ( isblank(*p) ) p ++;
d = section;
while ( *p ) {
if ( isspace(*p) ) break;
if ( d - section >= MAX_STR ) break;
*d ++ = *p ++;
}
*d = '\0';
while ( isblank(*p) ) p ++;
if ( *p != '\n' ) {
d = date;
while ( *p ) {
if ( isspace(*p) ) break;
if ( d - date >= MAX_STR ) break;
*d ++ = *p ++;
}
*d = '\0';
}
else {
time_t tt = time(0); // get time now
struct tm *t = localtime(&tt);
sprintf(date, "%s %d %d", month[t->tm_mon], t->tm_mday, t->tm_year+1900);
}
return p;
}
#define dcopy(c) { for ( const char *s = (c); *s; *d ++ = *s ++ ); }
void md2roff(const char *docname, const char *source) {
const char *p = source, *pnext, *pstart;
char *dest, *d;
bool bline = true, bcode = false;
bool bold = false, italics = false;
bool inside_list = false;
bool title_level = 0;
char secname[256], appname[256], appsec[256], appdate[256];
stk_list_p = 0; // reset stack
secname[0] = '\0';
dest = (char *) malloc(64*1024);
d = dest;
puts(".\\# roff document");
puts(".\\# DO NOT MODIFY THIS FILE! It was generated by md2roff");
switch ( mpack ) {
case mp_mm:
puts(".do mso m.tmac"); // mm package, AL BL DL LI LE
break;
case mp_ms:
puts(".do mso ms.tmac"); // ms package
while ( isspace(*p) ) p ++;
if ( p[0] == '#' && isblank(p[1]) ) {
puts(".TL");
p += 2;
char *pn = (char *) p;
while ( *pn && *pn != '\n' ) pn ++;
*pn = '\0';
puts(p);
p = (const char*) (pn + 1);
puts(".\\# .AU");
puts(".\\# Author");
puts(".\\# .AI");
puts(".\\# Author's institution(s)");
puts(".\\# .ND date");
puts(".\\# .AB");
puts(".\\# Abstract; to be placed on the cover sheet of a paper.");
puts(".\\# Line length is 5/6 of normal; use .11 here to change.");
puts(".\\# .AE");
puts(".PP");
}
break;
case mp_mdoc:
case mp_man:
if ( mpack == mp_mdoc )
puts(".do mso mdoc.tmac"); // BSD man
else
puts(".do mso man.tmac"); // Linux man
while ( isspace(*p) ) p ++;
if ( p[0] == '#' && isblank(p[1]) ) {
p = get_man_header(p+2, appname, appsec, appdate);
if ( mpack == mp_mdoc ) {
printf(".Dd $Mdocdate: %s $\n", appdate);
printf(".Dt %s %s\n", appname, appsec);
printf(".Os\n");
while ( *p != '\n' ) p ++;
}
else { // linux man
printf(".TH %s %s %s", appname, appsec, appdate);
if ( *p != '\n' )
p = println(p);
else
printf("\n");
}
while ( isspace(*p) ) p ++;
}
else { // no header specified
time_t tt = time(0); // get time now
struct tm *t = localtime(&tt);
strcpy(appname, docname);
if ( mpack == mp_mdoc ) {
printf(".Dd $Mdocdate: %s %d %d $\n",
month[t->tm_mon], t->tm_mday, t->tm_year+1900);
printf(".Dt %s %s\n", appname, "7");
printf(".Os\n");
}
else {
printf(".TH %s 7 %d-%02d-%02d document\n", docname,
t->tm_year+1900, t->tm_mon+1, t->tm_mday);
}
}
break;
case mp_mom:
puts(".do mso mom.tmac"); // mom
printf(".TITLE \"%s\"\n", docname);
printf(".AUTHOR \"md2roff\"\n");
printf(".PAPER A4\n");
printf(".PRINTSTYLE TYPESET\n");
printf(".START\n");
break;
}
while ( *p ) {
//////////////////////////////////
// inside code block
//////////////////////////////////
if ( bcode ) {
d = flushln(d, dest); // we dont care
if ( strncmp(p, "```", 3) == 0 ) { // end of code-block
p += 3;
while ( *p != '\n' ) p ++;
if ( *p == '\n' ) p ++;
bcode = false;
roff(cblock_end);
d = flushln(d, dest);
continue;
}
else {
bool xchg_dot = false;
if ( *p == '.' ) {
if ( mpack == mp_mom )
puts(".ESC_CHAR !");
else
puts(".cc !");
xchg_dot = true;
}
p = println(p);
if ( xchg_dot ) {
if ( mpack == mp_mom )
puts(".ESC_CHAR .");
else
puts("!cc .");
}
continue;
}
}
//////////////////////////////////
// ignore escape characters
if ( *p == '\\' ) {
p ++;
switch (*p) {
case 'n': *d ++ = '\n'; break;
case 'r': *d ++ = '\r'; break;
case 't': *d ++ = '\t'; break;
case 'f': *d ++ = '\f'; break;
case 'b': *d ++ = '\b'; break;
case 'a': *d ++ = '\a'; break;
case 'e': *d ++ = '\033'; break;
default:
*d ++ = *p;
}
p ++;
bline = false;
continue;
}
//////////////////////////////////
// beginning of line
//////////////////////////////////
if ( bline ) {
bline = false;
bq_level = 0;
if ( *p == '>' ) { // open blockquote
while ( *p == '>' ) { p ++; bq_level ++; }
d = flushln(d, dest);
roff(none);
d = flushln(d, dest);
}
//
if ( *p == '\n' ) { // empty line
d = flushln(d, dest);
if ( stk_list_p ) {
roff(li_end);
roff(lst_close);
stk_list_p --;
}
roff(par_end);
bline = true;
p ++;
continue;
}
else if ( *p == '#' ) { // header
d = flushln(d, dest);
pnext = strchr(p+1, '\n');
if ( pnext ) {
if ( *(pnext-1) != '#' ) {
int level = 0;
while ( *p == '#' ) { level ++; p ++; }
while ( *p == ' ' || *p == '\t' ) p ++;
switch ( level ) {
case 1: roff(new_sh); break; // TH?
case 2: {
const char *s;
char *n;
for ( s = p, n = secname; *s != '\n'; *n ++ = *s ++ );
*n = '\0';
if ( man_ofc ) {
if ( strcmp(secname, "COPYRIGHT") == 0 \
|| strcmp(secname, "AUTHOR") == 0 \
|| strcmp(secname, "HOMEPAGE") == 0 \
|| strcmp(secname, "REPORTING BUGS") == 0 \
|| strcmp(secname, "AUTHOR") == 0 \
|| strcmp(secname, "AUTHORS") == 0 )
write_lock = 1;
else
write_lock = 0;
}
if ( !write_lock ) roff(new_sh);
}
break;
case 3: roff(new_ss); break;
case 4:
default:
if ( mpack == mp_ms ) {
roff(new_ss);
break;
}
if ( mpack == mp_man ) {
d = flushln(d, dest);
printf(".TP\n");
int state = 'R';
dcopy("\\fB");
while ( isblank(*p) ) p ++;
while ( isalnum(*p) )
*d ++ = *p ++;
dcopy("\\fR");
while ( *p ) {
// continue to the next line
if ( *p == '\\' ) {
while ( *p && *p != '\n' ) p ++;
if ( *p == '\n' ) p ++;
continue;
}
// end of string, reset to defaults and exit loop
if ( *p == '\n' || *p == '\r' ) {
if ( state != 'R' )
dcopy("\\fR");
break;
}
// other characters
switch ( *p ) { // separator, nocolor
case ' ': case '\t':
case '[': case '{': case '(':
case ']': case '}': case ')':
case ',': case '|': case '.':
case '=':
if ( state != 'R' ) {
dcopy("\\fR");
state = 'R';
}
*d ++ = *p ++;
break;
case '+': case '!':
if ( state != 'B' ) {
dcopy("\\fB");
state = 'B';
}
*d ++ = *p ++;
break;
case '-': // short or long option, bold
if ( state != 'B' ) {
dcopy("\\fB");
state = 'B';
}
if ( p[1] == '-' ) // double minus
*d ++ = *p ++;
*d ++ = *p ++;
while ( isalnum(*p) )
*d ++ = *p ++;
break;
default: // normal parameter, italics
if ( state != 'I' ) {
dcopy("\\fI");
state = 'I';
}
*d ++ = *p ++;
}
}
d = flushln(d, dest);
}
else
roff(new_s4);
continue;
}
p = println(p);
if ( mpack == mp_ms )
puts(".PP");
bline = true;
continue;
}
else {
roff(box_open);
roff(ln_brk);
p = println(p);
roff(ln_brk);
roff(box_close);
continue;
}
}
}
else if ( mpack == mp_man && strcmp(secname, "SYNOPSIS") == 0
&& (opt_name_style == 2 || strncmp(p, KEY_GNUSYN, strlen(KEY_GNUSYN)) == 0) ) { // SYNTAX BLOCK (.SY/.YS)
bool first;
d = flushln(d, dest);
if ( opt_name_style != 2 )
p += strlen(KEY_GNUSYN);
dcopy(".SY ");
while ( isspace(*p) ) p ++;
while ( *p && *p != '\n' ) *d ++ = *p ++;
if ( *p ) *d ++ = *p ++;
while ( *p ) {
if ( !isblank(*p) ) {
if ( *p == '\n' )
break;
else {
int mode = 2;
if ( *p == '-' ) mode = 1;
if ( mode == 1 )
dcopy(".OP \\")
else
dcopy(".RI ")
first = true;
while ( *p && *p != '\n' ) {
if ( *p == ' ' && first ) {
first = false;
if ( mode == 2 )
dcopy("\\ ");
}
else if ( !first ) {
if ( *p == ' ' ) {
dcopy("\\fR\\");
*d ++ = *p ++;
dcopy("\\f");
*d ++ = (mode == 1) ? 'I' : 'R';
continue;
}
else if ( strchr("[].-{}|", *p) ) {
dcopy("\\f");
*d ++ = (mode == 1) ? 'B' : 'R';
while ( strchr("[].-{}|", *p) )
*d ++ = *p ++;
dcopy("\\f");
*d ++ = (mode == 1) ? 'I' : 'R';
continue;
}
}
*d ++ = *p ++;
}
if ( *p ) *d ++ = *p ++;