-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMarkdown.gi
More file actions
233 lines (216 loc) · 9.19 KB
/
Markdown.gi
File metadata and controls
233 lines (216 loc) · 9.19 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
#############################################################################
##
## AutoDoc package
##
## Copyright 2012-2016
## Sebastian Gutsche, University of Kaiserslautern
## Max Horn, Justus-Liebig-Universität Gießen
##
## Licensed under the GPL 2 or later.
##
#############################################################################
##
InstallGlobalFunction( INSERT_IN_STRING_WITH_REPLACE,
function( string, new_string, position, nr_letters_to_be_replaced )
return Concatenation(
string{[ 1 .. position - 1 ]},
new_string,
string{[ position + nr_letters_to_be_replaced .. Length( string ) ]}
);
end );
##
InstallGlobalFunction( CONVERT_LIST_OF_STRINGS_IN_MARKDOWN_TO_GAPDOC_XML,
function( string_list )
local i, current_list, current_string, max_line_length,
current_position, already_in_list, command_list_with_translation, beginning,
commands, position_of_command, insert, beginning_whitespaces, temp, string_list_temp, skipped,
already_inserted_paragraph, in_list, in_item,
str, pos, replace_start, replace_end, symbol_start, symbol_end, escape, symbol,
args_start, args_end, args, j, ref_tag;
## Check for paragraphs by turning an empty string into <P/>
already_inserted_paragraph := false;
for i in [ 1 .. Length( string_list ) ] do
if NormalizedWhitespace( string_list[ i ] ) = "" then
if already_inserted_paragraph = false then
string_list[ i ] := "<P/>";
already_inserted_paragraph := true;
fi;
else
already_inserted_paragraph := false;
fi;
i := i + 1;
od;
## We need to find lists. Lists are indicated by a beginning
## *, -, or +. Lists can be nested. Save list as list of strings,
## and at the same time, concatenate all the other strings
## FIXME: @Max: where are my regular expressions?
## Do this in several iterations
max_line_length := Maximum( List( string_list, Length ) );
current_position := 1;
while current_position < max_line_length do
already_in_list := false;
i := 1;
skipped := false;
## maybe make the first line marked by definition?
while i <= Length( string_list ) do
if PositionSublist( string_list[ i ], "<![CDATA[" ) <> fail then
skipped := true;
fi;
if PositionSublist( string_list[ i ], "]]>" ) <> fail then
skipped := false;
i := i + 1;
continue;
fi;
if skipped = true then
i := i + 1;
continue;
fi;
if PositionSublist( string_list[ i ], "* " ) = current_position
or PositionSublist( string_list[ i ], "+ " ) = current_position
or PositionSublist( string_list[ i ], "- " ) = current_position then
if not ForAll( [ 1 .. current_position - 1 ], j -> string_list[ i ][ j ] = ' ' ) then
i := i + 1;
continue;
fi;
if already_in_list = false then
Add( string_list, "<Item>", i );
Add( string_list, "<List>", i );
i := i + 2;
string_list[ i ] := string_list[ i ]{[ current_position + 2 .. Length( string_list[ i ] ) ]};
already_in_list := true;
else
Add( string_list, "<Item>", i );
Add( string_list, "</Item>", i );
i := i + 2;
string_list[ i ] := string_list[ i ]{[ current_position + 2 .. Length( string_list[ i ] ) ]};
fi;
## find out if line has to be marked
## THIS is buggy. Discuss this
## FIXME: This causes strange problems with GAPDoc.
# if PositionSublist( string_list[ i ], "**" ) = 1 then
# string_list[ i ] := string_list[ i ]{[ 3 .. Length( string_list[ i ] ) ]};
# temp := string_list[ i ];
# string_list[ i ] := string_list[ i - 1 ];
# string_list[ i - 1 ] := temp;
# Add( string_list, "<Mark>", i - 1 );
# Add( string_list, "</Mark>", i + 1 );
# i := i + 2;
# fi;
elif already_in_list = true and PositionSublist( string_list[ i ], " " ) > current_position then
already_in_list := false;
Add( string_list, "</List>", i );
Add( string_list, "</Item>", i );
i := i + 2;
fi;
i := i + 1;
od;
if already_in_list = true then
Add( string_list, "</Item>" );
Add( string_list, "</List>" );
fi;
current_position := current_position + 1;
od;
# Remove <P/> if in List but not in item
in_list := 0;
in_item := 0;
for current_position in [ 1 .. Length( string_list ) ] do
if PositionSublist( string_list[ current_position ], "<List>" ) <> fail then
in_list := in_list + 1;
fi;
if PositionSublist( string_list[ current_position ], "</List>" ) <> fail then
in_list := in_list - 1;
fi;
if PositionSublist( string_list[ current_position ], "<Item>" ) <> fail then
in_item := in_item + 1;
fi;
if PositionSublist( string_list[ current_position ], "</Item>" ) <> fail then
in_item := in_item - 1;
fi;
if in_item < in_list and string_list[ current_position ] = "<P/>" then
string_list[ current_position ] := "";
fi;
od;
## Find commands
command_list_with_translation := [ [ "$$", "Display" ],
[ "$", "Math" ],
[ "`", "Code" ],
[ "**", "Emph" ],
[ "__", "Emph" ] ];
## special handling for \$
for i in [ 1 .. Length( string_list ) ] do
string_list[ i ] := ReplacedString( string_list[ i ], "\\$", "$" );
od;
for commands in command_list_with_translation do
beginning := true;
skipped := false;
for i in [ 1 .. Length( string_list ) ] do
if PositionSublist( string_list[ i ], "<![CDATA[" ) <> fail then
skipped := true;
fi;
if PositionSublist( string_list[ i ], "]]>" ) <> fail then
skipped := false;
fi;
if skipped = true then
continue;
fi;
while PositionSublist( string_list[ i ], commands[ 1 ] ) <> fail do
position_of_command := PositionSublist( string_list[ i ], commands[ 1 ] );
if beginning = true then
insert := Concatenation( "<", commands[ 2 ], ">" );
else
insert := Concatenation( "</", commands[ 2 ], ">" );
fi;
string_list[ i ] := INSERT_IN_STRING_WITH_REPLACE( string_list[ i ], insert, position_of_command, Length( commands[ 1 ] ) );
beginning := not beginning;
od;
od;
if beginning = false then
Error( "did you forget some ", commands[ 1 ] );
fi;
od;
# #foo -> <Ref Subsect="foo" />
for i in [ 1 .. Length( string_list ) ] do
str := string_list[ i ];
pos := Position( str, '#' );
while pos <> fail do
replace_start := pos;
symbol_start := pos + 1;
escape := false;
for j in [ symbol_start .. Length( str ) ] do
if escape then
escape := false;
elif str[ j ] = '\\' then
escape := true;
elif not ( IsDigitChar( str[ j ] ) or IsAlphaChar( str[ j ] ) or str[ j ] in "@_" ) then
break;
fi;
od;
symbol_end := j - 1;
symbol := str{ [ symbol_start .. symbol_end ] };
if str[ j ] = '[' then
args_start := j + 1;
j := Position( str, ']', args_start );
if j = fail then
Error( "missing ']'" );
fi;
args_end := j - 1;
args := SplitString( str{ [ args_start .. args_end ] }, "," );
else
args := fail;
fi;
replace_end := j;
ref_tag := Concatenation( "<Ref Func=\"", symbol, "\" " );
if args <> fail then
ref_tag := Concatenation( ref_tag, "Label=\"for ",
JoinStringsWithSeparator( args, ", " ),
"\" " );
fi;
ref_tag := Concatenation( ref_tag, "/>" );
str := INSERT_IN_STRING_WITH_REPLACE( str, ref_tag, replace_start,
replace_end - replace_start + 1 );
pos := Position( str, '#' );
od;
string_list[ i ] := str;
od;
return string_list;
end );