-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx2sphinx.cpp
2018 lines (1693 loc) · 47.9 KB
/
mx2sphinx.cpp
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
/*
* Converter from MX into Sphinx source documentation formats.
*
* Copyright (C) 2010 Alex Dubov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string>
#include <fstream>
#include <iostream>
#include <ext/rope>
#include <functional>
#include <initializer_list>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/iterator/iterator_traits.hpp>
#include <boost/xpressive/xpressive_static.hpp>
using namespace std;
namespace bx = boost::xpressive;
namespace bf = boost::filesystem;
//#define DEBUG_MACROS
static const unsigned int tab_size(8);
static string pad_string(unsigned int p_size)
{
return string(p_size / tab_size, '\t')
+ string(p_size % tab_size, ' ');
}
template <typename pair_t>
static bool is_blank(const pair_t &in)
{
static const bx::sregex space_expr(bx::bos >> *bx::_s >> bx::eos);
return (bx::regex_match(in.first, in.second, space_expr));
}
template <typename pair_t>
static string trim(const pair_t &in)
{
static const bx::sregex trim_expr(
bx::bos >> *bx::_s >> (bx::s1 = -*bx::_) >> *bx::_s >> bx::eos
);
bx::smatch what;
if (bx::regex_match(in.first, in.second, what, trim_expr))
return what.str(1);
else
return string();
}
const bx::sregex brace_expr(
(~bx::after('\\') >> '{')
>> *(bx::by_ref(brace_expr)
| bx::keep(*(~(bx::set = '{','}')
| (bx::after('\\')
>> (bx::set = '{','}')))))
>> (~bx::after('\\') >> '}')
);
const bx::sregex paren_expr(
(~bx::after('\\') >> '(')
>> *(bx::by_ref(paren_expr)
| bx::keep(*(~(bx::set = '(',')')
| (bx::after('\\')
>> (bx::set = '(',')')))))
>> (~bx::after('\\') >> ')')
);
const bx::sregex csv_char_expr(
(bx::after('\\') >> ',') | ~bx::as_xpr(',')
);
const bx::sregex csv_simple_expr(
(bx::bos | (~bx::after('\\') >> ','))
>> (bx::s1 = *csv_char_expr)
);
const bx::sregex csv_paren_expr(
(bx::bos | (~bx::after('\\') >> ','))
>> (bx::s1 = *(bx::keep(paren_expr) | (bx::after('\\') >> ',')
| ~bx::as_xpr(',')))
);
template <typename pair_t>
static void split_csv(vector<string> &out, const pair_t &in,
const bx::sregex &expr = csv_simple_expr)
{
bx::sregex_iterator b(in.first, in.second, expr), e;
for (; b != e; ++b)
out.push_back(b->str(1));
}
template <typename pair_t>
static void parse_href(vector<string> &out, const pair_t &in)
{
static const bx::sregex href_expr(
bx::bos >> *bx::_s >> "<a" >> +bx::_s >> -*bx::_ >> "href"
>> *bx::_s >> '=' >> *bx::_s
>> ((~bx::after('\\') >> '"' >> (bx::s1 = -*bx::_)
>> ~bx::after('\\') >> '"')
| (bx::s1 = *(~bx::_s
| (bx::after('\\') >> bx::_s))))
>> -*bx::_ >> '>' >> *bx::_s >> (bx::s2 = -*bx::_)
>> *bx::_s >> "</a>" >> *bx::_
);
bx::smatch what;
if (!regex_match(in.first, in.second, what, href_expr))
out.push_back(trim(in));
else {
out.push_back(what.str(1));
out.push_back(what.str(2));
}
}
static void null_comment(function<void (const string &)> out,
const vector<string> &in)
{
}
static void c_comment(function<void (const string &)> out,
const vector<string> &in)
{
if (in.size() == 1)
out("/* " + in[0] + "*/");
else if (in.size() > 1) {
out("/*");
BOOST_FOREACH(const string &s, in) {
if (!s.empty())
out(" * " + s);
else
out(" *" + s);
}
out(" */");
}
}
static void null_textref(function<void (const string &)> out,
const string &name, unsigned int pos)
{
}
static void c_textref(function<void (const string &)> out,
const string &name, unsigned int pos)
{
out((boost::format("#line %1% \"%2%\"") % pos % name).str());
};
typedef function<void (function<void (const string &)>,
const vector<string>&)> comment_formatter_t;
typedef function<void (function<void (const string &)>,
const string&, unsigned int)> textref_formatter_t;
static const map<string, comment_formatter_t> comment_formatters {
{"c", c_comment},
{"h", c_comment}
};
static const map<string, textref_formatter_t> textref_formatters {
{"c", c_textref}
};
/* A simplistic one. */
template <typename pair_t>
static size_t find_print_length(const pair_t &in)
{
size_t rv(0);
for (auto b = in.first; b < in.second; ++b) {
if (*b == '\t')
rv += tab_size;
else
++rv;
}
return rv;
}
struct target {
struct line_mark {
target *t;
__gnu_cxx::crope::iterator begin;
__gnu_cxx::crope::iterator end;
unsigned int src_pos;
string padding;
unsigned int line_num;
line_mark(target *t_, const string &line, unsigned int src_pos_)
: t(t_),
begin(t->body.mutable_end()),
src_pos(src_pos_),
padding(t->padding),
line_num(t->line_cnt) {
t->add_line(line);
end = t->body.mutable_end();
}
};
struct line_ref {
target *t;
__gnu_cxx::crope::iterator begin;
__gnu_cxx::crope::iterator end;
string padding;
string tag;
pair <size_t, size_t> lines;
function<string (size_t, size_t)> editor;
line_ref(target *t_, const string &tag_,
function<string (size_t, size_t)> editor_,
size_t s, size_t e)
: t(t_),
begin(t->body.mutable_end()),
padding(t->padding),
tag(tag_),
lines(s, e),
editor(editor_) {
t->add_line(editor(s, e));
end = t->body.mutable_end();
}
};
__gnu_cxx::crope body;
string padding;
unsigned int line_cnt;
vector< boost::variant<line_mark, line_ref> > line_marks;
map<size_t, size_t> line_exp;
comment_formatter_t comment_formatter;
textref_formatter_t textref_formatter;
target(const string &tag = string())
: line_cnt(0),
comment_formatter(null_comment),
textref_formatter(null_textref) {
auto c_iter(comment_formatters.find(tag));
if (c_iter != comment_formatters.end())
comment_formatter = c_iter->second;
auto t_iter(textref_formatters.find(tag));
if (t_iter != textref_formatters.end())
textref_formatter = t_iter->second;
}
void set_indent(unsigned int indent = 0) {
padding = pad_string(indent);
}
void add_line(const string &line = string()) {
if (!line.empty()) {
body += padding.c_str();
body += line.c_str();
}
body += '\n';
++line_cnt;
}
void add_line_mark(const string &line, unsigned int src_pos) {
line_marks.push_back(line_mark(this, line, src_pos));
line_exp.insert(make_pair(line_cnt, 1));
}
void add_line_ref(const string &tag,
function<string (size_t, size_t)> editor,
size_t s, size_t e) {
line_marks.push_back(line_ref(this, tag, editor, s, e));
}
};
struct mx_context {
typedef function<void (mx_context*, const string &line)> tag_handler_t;
typedef pair<string, unsigned int> macro_line_t;
typedef list<macro_line_t> line_block_t;
struct macro_t {
line_block_t lines;
string f_name;
unsigned int b_pos, e_pos;
};
enum sec_level_t {
TITLE_LVL = 0,
MODULE_LVL,
SECT_LVL,
SUBSECT_LVL,
PAR_LVL
};
enum tag_level_t {
GEN_MARK_TAG = 0,
DOC_MARK_TAG,
SUB_BLK_TAG,
MACRO_REF_TAG,
MACRO_ARG_TAG,
};
struct in_file {
bf::path name;
string base_name;
unsigned int line_cnt;
ifstream s;
in_file(bf::path name_)
: name(name_),
base_name(bf::path(name.filename()).replace_extension()
.file_string()),
line_cnt(0),
s(name.file_string().c_str(), ios::binary) {}
string location() {
return (boost::format("%1%:%2%")
% name.file_string() % line_cnt).str();
}
};
static map<string, mx_context::tag_handler_t> mx_tags;
static map<string, mx_context::tag_handler_t> mx_item_tags;
static map<string, mx_context::tag_handler_t> info_formatters;
static vector<char> sec_heads;
static const bx::sregex doc_mark_expr, text_mark_expr, sub_blk_expr,
macro_ref_expr, macro_arg_expr, dead_macro_expr,
gen_mark_expr, tag_class_expr, doc_tag_expr;
static const bx::placeholder<tag_level_t> tag_class;
boost::ptr_vector<in_file> in;
string ref_name;
map<string, target> out_files;
decltype(out_files.begin()) doc_iter, t_iter;
stack< pair<string, tag_handler_t> > envs;
boost::optional<tag_handler_t> auto_end;
unsigned int end_pos;
vector<bf::path> includes;
set<string> defines;
stack< pair<string, unsigned int> > prefixes;
int min_sec_lvl, abs_sec_lvl, rel_sec_lvl;
int image_cnt;
bool modulename_set;
macro_line_t saved_line;
vector<string> info_lines, extra_lines;
map<string, macro_t> macros;
decltype(macros.begin()) c_macro;
char get_level_head(sec_level_t lvl);
void comment(const string &line);
void info_block(const string &line);
void end_info_block(const string &line);
void basename(const string &line);
void macro_def(const string &line);
void end_macro_def(const string &tag);
void include(const string &line);
void author(const string &line);
void version(const string &line);
void date(const string &line);
void noindent(const string &line);
void title(const string &line);
void module(const string &line);
void section(const string &line);
void subsection(const string &line);
void paragraph(const string &line);
void node(const string &line);
void end(const string &line);
void menu(const string &line);
void end_menu(const string &line);
void example(const string &line);
void end_example(const string &line);
void qtex(const string &line);
void end_qtex(const string &line);
void tex(const string &line);
void iftex(const string &line);
void end_tex(const string &line);
void ifset(const string &line);
void ifclear(const string &line);
void end_if(const string &line);
void verbatim(const string &line);
void end_verbatim(const string &line);
void itemize(const string &line);
void enumerate(const string &line);
void end_itemize(const string &line);
void table(const string &line);
void end_table(const string &line);
void multitable(const string &line);
void end_multitable(const string &line);
void subblock(const string &line);
void end_subblock(const string &line);
void subblock1(const string &line);
void end_subblock1(const string &line);
void item(const string &line);
void tab(const string &line);
void item_itemize(const string &line);
void item_table(const string &line);
void generic_tag(const string &tag, const string &line);
void end_generic_tag(const string &tag);
void expand_macros(line_block_t &out, const macro_line_t &in,
const target &t, unsigned int indent = 0,
unsigned int pass = 1);
string replace_doc_tags(const bx::smatch &what);
string replace_text_tags(const bx::smatch &what);
void parse_line_include(const string &line);
void parse_line_literal(const string &line);
void parse_line_doc(const string &line);
void add_line_markup(const string &line);
void add_line_text(const string &line);
void add_line_expand(const string &line);
void add_line_macro(const string &line);
void add_line_menu(const string &line);
void add_line_asis(const string &line);
void add_line_info(const string &line);
void add_line_noop(const string &line);
void add_line_c_comment(const string &line);
tag_handler_t parse_line;
tag_handler_t add_line, add_line_prev;
struct late_expand : public boost::static_visitor<> {
mx_context &context;
size_t delta;
late_expand(mx_context &context_)
: context(context_),
delta(0) {}
void operator()(target::line_mark &m);
void operator()(target::line_ref &m) {
m.begin += delta;
m.end += delta;
}
};
struct ref_edit : public boost::static_visitor<> {
mx_context &context;
size_t delta;
ssize_t b_delta;
ref_edit(mx_context &context_)
: context(context_),
delta(0),
b_delta(0){}
void operator()(target::line_mark &m) {}
void operator()(target::line_ref &m);
};
mx_context(const vector<string> &includes_, const string &doc_tag,
const set<string> &defines_);
void write_out(const bf::path &prefix);
};
map<string, mx_context::tag_handler_t> mx_context::mx_tags {
{"'", &mx_context::comment},
{"/", &mx_context::info_block},
{"f", &mx_context::basename},
{"=", &mx_context::macro_def},
{"include", &mx_context::include},
{"a", &mx_context::author},
{"v", &mx_context::version},
{"d", &mx_context::date},
{"noindent", &mx_context::noindent},
{"t", &mx_context::title},
{"*", &mx_context::module},
{"+", &mx_context::section},
{"section", &mx_context::section},
{"subsection", &mx_context::subsection},
{"-", &mx_context::paragraph},
{"node", &mx_context::node},
{"menu", &mx_context::menu},
{"example", &mx_context::example},
{"verbatim", &mx_context::verbatim},
{"itemize", &mx_context::itemize},
{"enumerate", &mx_context::enumerate},
{"table", &mx_context::table},
{"multitable", &mx_context::multitable},
{"end", &mx_context::end},
{"item", &mx_context::item},
{"tab", &mx_context::tab},
{"{", &mx_context::subblock},
{"}", &mx_context::end_subblock},
{"(", &mx_context::subblock1},
{")", &mx_context::end_subblock1},
{"T", &mx_context::qtex},
{"tex", &mx_context::tex},
{"iftex", &mx_context::iftex},
{"ifset", &mx_context::ifset},
{"ifclear", &mx_context::ifclear}
};
map<string, mx_context::tag_handler_t> mx_context::mx_item_tags {
{"itemize", &mx_context::item_itemize},
{"enumerate", &mx_context::item_itemize},
{"table", &mx_context::item_table},
{"multitable", &mx_context::item_table}
};
vector<char> mx_context::sec_heads{'#', '*', '=', '-', '^', '"'};
const bx::sregex mx_context::doc_mark_expr(
'@' >> (((bx::s1 = (bx::set = '[', '%', '#', '`'))
>> (bx::s2 = -*bx::_) >> ~bx::after('\\')
>> '@' >> (bx::s4 = !bx::_d))
| ((((bx::s1 = 'e') >> "mph")
| ("ci" >> (bx::s1 = 't') >> 'e')
| ((bx::s1 = 's') >> "trong")
| ((bx::s1 = 'v') >> "erb")
| ((bx::s1 = 'u') >> "rl")
| ((bx::s1 = 'i') >> "mage")
| ('s' >> (bx::s1 = 'c'))
| ('c' >> (bx::s1 = 'o') >> "de"))
>> (bx::s2 = brace_expr))) >> (bx::s3 = !bx::_s)
);
const bx::sregex mx_context::text_mark_expr(
~bx::after('\\') >> '@' >> (bx::s1 = '`') >> (bx::s2 = -*bx::_)
>> ~bx::after('\\') >> '@' >> (bx::s3 = bx::_d)
);
const bx::sregex mx_context::sub_blk_expr(
'@' >> (bx::s1 = (bx::set = '{', '}', '(', ')'))
);
const bx::sregex mx_context::macro_ref_expr(
"@:" >> (bx::s1 = -+bx::_)
>> (((bx::s2 = paren_expr) >> !(~bx::after('\\') >> '@'))
| ((~bx::after('\\') >> '@')
| (!((bx::s3 = '\\') >> bx::_s) >> bx::eos)))
);
const bx::sregex mx_context::macro_arg_expr(
'@' >> !((bx::s3 = '?') >> '@')
>> ((bx::s2 = bx::_d) | ('[' >> (bx::s2 = +bx::_d) >> ']'))
);
const bx::sregex mx_context::dead_macro_expr(
"@!!" >> (bx::s1 = -+bx::_)
>> ((bx::s2 = paren_expr) >> !(~bx::after('\\') >> '@')
| ((~bx::after('\\') >> '@') | bx::eos))
);
const bx::sregex mx_context::gen_mark_expr(
bx::bos >> '@' >> (bx::s2 = *~bx::_s) >> *bx::_s
>> (bx::s3 = -*bx::_) >> *bx::_s >> bx::eos
);
const bx::placeholder<mx_context::tag_level_t> mx_context::tag_class = {{}};
const bx::sregex mx_context::tag_class_expr(
bx::bos >> *bx::_s >> ((macro_arg_expr)[tag_class = MACRO_ARG_TAG]
| (macro_ref_expr)[tag_class = MACRO_REF_TAG]
| (doc_mark_expr)[tag_class = DOC_MARK_TAG]
| (sub_blk_expr)[tag_class = SUB_BLK_TAG])
);
const bx::sregex mx_context::doc_tag_expr(
macro_arg_expr | doc_mark_expr
);
char mx_context::get_level_head(sec_level_t lvl)
{
char rv;
if (min_sec_lvl == -1)
min_sec_lvl = lvl;
if (lvl > abs_sec_lvl) {
if (rel_sec_lvl < (sec_heads.size() - 1))
++rel_sec_lvl;
} else if (lvl < abs_sec_lvl) {
rel_sec_lvl -= abs_sec_lvl - lvl;
if (rel_sec_lvl < min_sec_lvl)
rel_sec_lvl = min_sec_lvl;
}
abs_sec_lvl = lvl;
return sec_heads[rel_sec_lvl];
}
void mx_context::comment(const string &line)
{
/* Comments are ignored */
}
void mx_context::info_block(const string &line)
{
add_line = &mx_context::add_line_info;
auto_end = &mx_context::end_info_block;
}
void mx_context::end_info_block(const string &line)
{
add_line = &mx_context::add_line_markup;
auto_end.reset();
}
void mx_context::basename(const string &line)
{
if (!line.empty()) {
in.back().base_name = line;
/* sphinx will choke if there are multiple module lines */
if ((in.size() == 1) && !modulename_set) {
t_iter = doc_iter;
t_iter->second.add_line(".. module:: " + line);
t_iter->second.add_line();
modulename_set = true;
}
}
}
void mx_context::macro_def(const string &line)
{
if (line.empty())
return;
c_macro = macros.find(line);
if (c_macro == macros.end())
c_macro = macros.insert(make_pair(line, macro_t())).first;
else {
cerr << "macro " << line << " redefined at "
<< in.back().location() << endl;
c_macro->second.lines.clear();
}
c_macro->second.f_name = in.back().name.filename();
c_macro->second.b_pos = in.back().line_cnt + 1;
auto_end = &mx_context::end_macro_def;
add_line = &mx_context::add_line_macro;
}
static void unindent_lines(mx_context::line_block_t &m)
{
static const bx::sregex leading_space_expr(
bx::bos >> (bx::s1 = +bx::_s)
);
bx::smatch what;
size_t min_off(0);
vector<unsigned int> pads;
BOOST_FOREACH(auto &s, m) {
pads.push_back(0);
if (bx::regex_search(s.first, what, leading_space_expr)) {
if (!what.suffix().length())
s.first.clear();
else {
auto c_off(find_print_length(what[1]));
if (min_off && (min_off > c_off))
min_off = c_off;
else if (!min_off)
min_off = c_off;
if (!min_off)
break;
pads.back() = c_off;
s.first.assign(what.suffix());
}
} else if (!s.first.empty()) {
min_off = 0;
break;
}
}
auto c_line(m.begin());
BOOST_FOREACH(unsigned int pad, pads) {
if (pad)
c_line->first.insert(0, pad_string(pad - min_off));
++c_line;
}
}
void mx_context::end_macro_def(const string &line)
{
auto_end.reset();
if (in.size() == 1) {
t_iter = doc_iter;
string m_title("Macro: " + c_macro->first);
t_iter->second.add_line();
t_iter->second.add_line(m_title);
t_iter->second.add_line(string(m_title.size(),
get_level_head(SUBSECT_LVL)));
t_iter->second.add_line(".. code-block:: guess");
t_iter->second.add_line();
t_iter->second.set_indent(prefixes.empty()
? 3 : (prefixes.top().second + 3));
BOOST_FOREACH(const macro_line_t &s, c_macro->second.lines)
t_iter->second.add_line(s.first);
t_iter->second.set_indent(prefixes.empty()
? 0 : prefixes.top().second);
t_iter->second.add_line();
add_line = &mx_context::add_line_markup;
} else
add_line = &mx_context::add_line_noop;
c_macro->second.e_pos = in.back().line_cnt + 1;
unindent_lines(c_macro->second.lines);
c_macro = macros.end();
}
void mx_context::include(const string &line)
{
if (line.empty())
return;
BOOST_FOREACH(bf::path &p, includes) {
bf::path f(bf::system_complete(p / line));
if (exists(p)) {
in.push_back(new in_file(f));
if (in.back().s.is_open()) {
parse_line = &mx_context::parse_line_include;
add_line = &mx_context::add_line_noop;
return;
} else
in.pop_back();
}
}
cerr << "couldn't open include file " << line << " at "
<< in.back().location() << " - skipping." << endl;
}
void mx_context::author(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line(":Author: " + line);
}
}
void mx_context::version(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line(":Version: " + line);
}
}
void mx_context::date(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line(":Date: " + line);
}
}
void mx_context::noindent(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line(line);
}
void mx_context::title(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line(line);
t_iter->second.add_line(string(line.size(),
get_level_head(TITLE_LVL)));
if (ref_name.empty())
ref_name = line;
}
}
void mx_context::module(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line(line);
t_iter->second.add_line(string(line.size(),
get_level_head(MODULE_LVL)));
if (ref_name.empty())
ref_name = line;
}
}
void mx_context::section(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line(line);
t_iter->second.add_line(string(line.size(),
get_level_head(SECT_LVL)));
}
}
void mx_context::subsection(const string &line)
{
if (!line.empty()) {
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line(line);
t_iter->second.add_line(string(line.size(),
get_level_head(SUBSECT_LVL)));
}
}
void mx_context::paragraph(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line();
if (!line.empty()) {
t_iter->second.add_line(line);
t_iter->second.add_line(string(line.size(),
get_level_head(PAR_LVL)));
} else
t_iter->second.add_line();
}
void mx_context::node(const string &line)
{
vector<string> nodes;
split_csv(nodes, make_pair(line.begin(), line.end()));
bool e_line(false);
t_iter = doc_iter;
BOOST_FOREACH(const string &s, nodes) {
if (!s.empty()) {
if (!e_line) {
t_iter->second.add_line();
e_line = true;
}
t_iter->second.add_line((boost::format(".. _%1%:")
% s).str());
}
}
if (e_line)
t_iter->second.add_line();
}
void mx_context::menu(const string &line)
{
envs.push(make_pair("menu", &mx_context::end_menu));
t_iter = doc_iter;
t_iter->second.add_line();
parse_line = &mx_context::parse_line_literal;
add_line = &mx_context::add_line_menu;
}
void mx_context::end_menu(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line();
add_line = &mx_context::add_line_markup;
}
void mx_context::example(const string &line)
{
envs.push(make_pair("example", &mx_context::end_example));
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line(".. code-block:: guess");
t_iter->second.add_line();
parse_line = &mx_context::parse_line_literal;
add_line = &mx_context::add_line_asis;
}
void mx_context::end_example(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line();
t_iter->second.add_line();
add_line = &mx_context::add_line_markup;
}
void mx_context::verbatim(const string &line)
{
envs.push(make_pair("verbatim", &mx_context::end_verbatim));
t_iter = doc_iter;
t_iter->second.add_line("::");
t_iter->second.add_line();
t_iter->second.set_indent(prefixes.empty() ? 3
: prefixes.top().second + 3);
parse_line = &mx_context::parse_line_literal;
add_line = &mx_context::add_line_asis;
}
void mx_context::end_verbatim(const string &line)
{
t_iter = doc_iter;
t_iter->second.set_indent(prefixes.empty() ? 0 : prefixes.top().second);
t_iter->second.add_line();
t_iter->second.add_line();
add_line = &mx_context::add_line_markup;
}
void mx_context::itemize(const string &line)
{
envs.push(make_pair("itemize", &mx_context::end_itemize));
unsigned int indent(prefixes.empty() ? 0 : prefixes.top().second
+ 3);
t_iter = doc_iter;
t_iter->second.add_line();
prefixes.push(make_pair("* ", indent));
}
void mx_context::enumerate(const string &line)
{
envs.push(make_pair("enumerate", &mx_context::end_itemize));
unsigned int indent(prefixes.empty() ? 0 : prefixes.top().second
+ 3);
t_iter = doc_iter;
t_iter->second.add_line();
prefixes.push(make_pair("#. ", indent));
}
void mx_context::end_itemize(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line();
prefixes.pop();
if (prefixes.empty())
t_iter->second.set_indent(0);
else
t_iter->second.set_indent(prefixes.top().second);
}
/* Handled as a definition list */
void mx_context::table(const string &line)
{
envs.push(make_pair("table", &mx_context::end_table));
unsigned int indent(prefixes.empty() ? 0 : prefixes.top().second
+ 3);
t_iter = doc_iter;
t_iter->second.add_line();
prefixes.push(make_pair(string(), indent));
}
void mx_context::end_table(const string &line)
{
t_iter = doc_iter;
t_iter->second.add_line();
prefixes.pop();
if (prefixes.empty())
t_iter->second.set_indent(0);
else
t_iter->second.set_indent(prefixes.top().second);
}
void mx_context::multitable(const string &line)
{
envs.push(make_pair("multitable", &mx_context::end_multitable));
unsigned int indent(prefixes.empty() ? 0 : prefixes.top().second
+ 3);