-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfendo.markup.fendo.code.fs
191 lines (147 loc) · 5.5 KB
/
fendo.markup.fendo.code.fs
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
.( fendo.markup.fendo.code.fs ) cr
\ This file is part of Fendo
\ (http://programandala.net/en.program.fendo.html).
\ This file defines the Fendo markup for code.
\ Last modified 20210429T1659+0200.
\ See change log at the end of the file.
\ Copyright (C) 2013,2014,2017,2018,2021 Marcos Cruz (programandala.net)
\ Fendo is free software; you can redistribute
\ it and/or modify it under the terms of the GNU General
\ Public License as published by the Free Software
\ Foundation; either version 2 of the License, or (at your
\ option) any later version.
\ Fendo 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://gnu.org/licenses>.
\ ==============================================================
\ Requirements {{{1
forth_definitions
\ require galope/s-comma.fs \ `s,`
require galope/trim.fs \ `trim`
fendo_definitions
require fendo/fendo.addon.source_code.common.fs
\ ==============================================================
\ Tools {{{1
fendo_definitions
: (##) ( "source code ##" -- )
begin parse-name dup
if 2dup s" ##" str=
dup >r 0= if escaped_source_code _echo else 2drop then r>
else 2drop refill 0= dup abort" Missing closing `##`" then
until ;
\ Parse an inline source code region.
\ XXX FIXME preserve spaces
\ XXX TODO highlight
: ####-line ( -- ca len )
\ Parse a new line from the current source code block.
read_source_line 0= abort" Missing closing `####`"
escaped_source_code ;
: "####"? ( ca len -- f )
\ Does the given string contains only "####"?
trim s" ####" str= ;
: ####-line? ( -- ca len f )
\ Parse a new line from the current source code block.
\ ca len = source code line
\ f = is it "####"?
####-line 2dup "####"?
\ cr ." exit stack in `####-line?` " .s key drop \ XXX INFORMER
;
: plain_####-zone ( "source code ####" -- )
begin
####-line? dup >r
if 2drop else escaped_source_code echo_line then r>
until
\ cr ." exit stack in `plain_####-zone` " .s key drop \ XXX INFORMER
;
\ Parse and echo a source code zone "as is".
: highlighted_####-zone ( "source code ####" -- )
new_source_code
begin
####-line? dup >r
if 2drop else append_source_code_line then r>
until source_code@ highlighted echo source_code_finished ;
\ Parse a source code zone, highlight and echo it.
: highlight_####-zone? ( -- f )
highlight? programming_language@ nip 0<> and ;
: (####) ( "source code ####" -- )
highlight_####-zone? if highlighted_####-zone
else plain_####-zone then ;
\ Parse and echo a source code zone.
\ ==============================================================
\ Markup {{{1
markup_definitions
: ## ( -- )
<code> (##) </code> ;
\ Open and close an inline <code> region.
: #### ( -- )
block_source_code{ (####) }block_source_code ;
\ Open and close a block <code> region.
\ ==============================================================
\ Custom code markup {{{1
0 [if]
The website application can create custom
The programming language of a source code block (actually, the name of
a valid Vim filetype in the host OS) can be set his way:
<[ s" gforth" programming_language! ]>
####
... gforth code ...
####
But there's an easier alternative. First, the website application has
to define a custom markup this way:
code_markup: gforth
Then, the following simpler markup can be used instead:
####gforth
... gforth code ...
####
[then]
fendo_definitions
: code_inline_markup ( ca len -- )
2dup s" ##" 2swap s+ :create_markup s,
does> ( -- )
( dfa ) count programming_language!
[markup>order] ## [markup<order] ;
\ Create inline code markup for a specific Vim filetype.
\ ca len = Vim filetype (for syntax highlighting)
: code_block_markup ( ca len -- )
2dup s" ####" 2swap s+ :create_markup s,
does> ( -- )
( dfa ) count programming_language!
[markup>order] #### [markup<order] ;
\ Create block code markup for a specific Vim filetype.
\ ca len = Vim filetype (for syntax highlighting)
: code_markup: ( "name" -- )
parse-name? abort" Expected Vim filetype name"
2dup code_inline_markup code_block_markup ;
\ Create inline and block code markups.
\ Used by the website application to create all
\ specific code markups used in the contents.
\ name = Vim filetype (for syntax highlighting)
.( fendo.markup.fendo.code.fs compiled ) cr
\ ==============================================================
\ Change log {{{1
\ 2014-04-21: Code moved from <fendo.markup.fendo.fs>.
\
\ 2014-04-22: Fix: silly bug.
\
\ 2014-12-12: Fix: `source_code_finished` has been added at the end of
\ `highlighted_####-zone`. This obscure bug made caused
\ `programming_language$` to be preserved after source code blocks, so
\ overwritting the automatic detection done by the next `source_code`
\ addon, in the current or next page. In order to call
\ `source_code_finished`, <fendo/fendo.addon.source_code.common.fs>
\ has been modifed and included.
\
\ 2017-06-22: Update source style, layout and header.
\
\ 2017-09-18: Fix documentation.
\
\ 2018-12-08: Update notation of Forth words in comments and strings.
\
\ 2021-04-29: Require `s,`, word removed from Gforth between versions
\ 0.7.9_20201231 and 0.7.9_20210422, and added to Galope.
\ vim: filetype=gforth