-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfendo.addon.markdown.fs
174 lines (132 loc) · 4.96 KB
/
fendo.addon.markdown.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
.( fendo.addon.markdown.fs) cr
\ This file is part of Fendo
\ (http://programandala.net/en.program.fendo.html).
\ This file is the Markdown addon. It provides words to include
\ contents in Markdown, either inline or from a file.
\ Last modified 20220123T1344+0100.
\ See change log at the end of the file.
\ Copyright (C) 2015,2017,2018,2019,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>.
\ Fendo is written in Forth (http://forth-standard.org)
\ with Gforth (<http://gnu.org/software/gforth>).
\ ==============================================================
\ Requirements {{{1
\ Pandoc must be installed in the system. See also:
\ <http://johnmacfarlane.net/pandoc/>.
forth_definitions
require galope/package.fs \ `package`, `private`, `public`, `end-package`
require galope/trim.fs \ `trim`
fendo_definitions
\ ==============================================================
\ Code {{{1
package fendo.addon.markdown
: input_file$ ( -- ca len )
source_dir $@ current_page source_file s" .md" s+ s+ ;
\ Return filename _ca len_ of the temporary file that
\ contains the Markdown code to be converted to HTML.
: output_file$ ( -- ca len )
input_file$ s" .html" s+ ;
\ Return filename _ca len_ of the temporary file that
\ contains the HTML output of the Markdown code, to be
\ included in the page.
: delete_files ( -- )
input_file$ delete-file throw
output_file$ delete-file throw ;
\ Delete the temporary files.
: >input_file ( ca len -- )
input_file$ w/o create-file throw
dup >r write-file throw r> close-file throw ;
\ Save the given contents to the file that Markdown will load as input.
\ ca len = text in Markdown format
: <output_file ( -- ca len )
output_file$ slurp-file ;
\ Get the content of the file that Markdown created as output.
\ ca len = contents in HTML format
public
: (pandoc_markdown_base_command)$ ( -- ca len )
s" pandoc -f markdown -t html -o " output_file$ s+ ;
: (markdown_base_command)$ ( -- ca len )
s" markdown > " output_file$ s+ ;
defer markdown_base_command$ ( -- ca len )
\ Base command to convert a Markdown file, not specified,
\ and save the result in `output_file$`.
\ The default action is the `markdown` program;
\ `pandoc` is provided as an alternative.
' (markdown_base_command)$ is markdown_base_command$
private
: markdown_command$ ( -- ca len )
markdown_base_command$ s" " s+ ;
\ Return the Markdown command without the input file.
: ((include_markdown)) ( ca1 len1 -- ca2 len2 )
markdown_command$ s" " s+ 2swap s+
system $? abort" The system markdown command failed"
<output_file ;
\ Return the contents _ca2 len2_ converted
\ from the Markdown file _ca1 len1_.
\ The header and footer of the file will be ignored.
: (include_markdown) ( ca1 len1 -- ca2 len2 )
file>local ((include_markdown)) ;
\ Return the contents _ca2 len2_ converted
\ from the Markdown file _ca1 len1_, which is converted to local.
\ The header and footer of the file will be ignored.
: parse_markdown ( "ccc<}markdown>" -- ca len )
s" "
begin
0 parse dup
if 2dup trim s" }markdown" str=
if 2drop exit else s+ true then
else 2drop s\" \n" s+ refill
then 0=
until ;
public
: include_markdown ( ca len -- )
(include_markdown) echo ;
\ doc{
\
\ include_markdown ( ca len -- )
\
\ Include contents in Markdown format from file _ca len_. The
\ header and footer of the file will be ignored.
\
\ See also: `markdownw{`, `include_asciidoctor`.
\
\ }doc
markup>current
: markdown{ ( "ccc<}markdown>" -- )
parse_markdown >input_file
input_file$ ((include_markdown)) echo ;
\ doc{
\
\ markdown{ ( "ccc<}markdown>" -- )
\
\ Parse contents in Markdown format, until "}markdown" is found (on
\ its own line). Then save the contents to a temporary file, convert
\ it to HTML and include the result into the current page.
\
\ See also: `include_markdown`, `asciidoctor{`.
\
\ }doc
end-package
.( fendo.addon.markdown.fs compiled) cr
\ ==============================================================
\ Change log {{{1
\ 2017-06-25: Start.
\
\ 2018-09-27: Use `package` instead of `module:`.
\
\ 2018-12-08: Update notation of Forth words in comments and strings.
\
\ 2019-01-19: Improve documentation. Use temporary files using the
\ source page filename with added extensions.
\
\ 2021-10-23: Replace "See:" with "See also:" in the documentation.
\ vim: filetype=gforth