-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfendo.files.fs
295 lines (248 loc) · 10.4 KB
/
fendo.files.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
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
.( fendo.files.fs ) cr
\ This file is part of Fendo
\ (http://programandala.net/en.program.fendo.html).
\ This file defines the file tools.
\ Last modified 202011160218.
\ See change log at the end of the file.
\ Copyright (C) 2013,2014,2015,2017,2018,2020 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/string-suffix-question.fs
fendo_definitions
\ ==============================================================
\ Target file {{{1
: (open_target) ( -- )
current_page
\ cr ." current_page in (open_target) = " dup . \ XXX INFORMER
target_path/file
\ cr ." target file = " 2dup type cr key drop \ XXX INFORMER
w/o create-file throw target_fid !
\ ." target file just opened: " \ XXX INFORMER
\ target_fid @ . cr \ XXX INFORMER
\ s" <!-- XXX -->" target_fid @ write-line throw \ XXX debugging
;
\ Open the target file (HTML or Atom).
: open_target ( -- )
echo>file? if (open_target) then ;
\ Open the target file (HTML or Atom), if needed.
: complete_iso_date ( ca1 len1 -- ca2 len2 )
dup case
10 of exit endof
7 of s" -01" s+ endof
4 of s" -01-01" s+ endof
abort" Wrong ISO date format"
endcase ;
: (set_file_mtime) ( ca1 len1 ca2 len2 -- )
complete_iso_date
\ 2dup cr ." Net ISO date = " type \ XXX INFORMER
s\" touch --date=\"" 2swap s+ s\" \" " s+ 2swap s+
system $? abort" Error in set_modification_time" ;
\ Set the modification time of file _ca1 len1_ to ISO date _ca2
\ len2_. The host operating system shell is used.
: set_file_mtime ( ca1 len1 ca2 len2 -- )
\ 2dup cr ." Raw ISO date = " type \ XXX INFORMER
dup if (set_file_mtime) else 2drop 2drop then ;
\ If _ca2 len2_ is not empty,
\ set the modification time of file _ca1 len1_ to ISO date _ca2
\ len2_.
false [if] \ XXX OLD
: set_modification_time ( ca len -- )
2>r s" touch --date=" current_page file_mtime s+ s" " s+ 2r> s+
system $? abort" Error in set_modification_time" ;
\ Set the modification time of the given file
\ to the correspondent metadata of the current page.
\ The host operating system shell is used.
[endif]
: set_modification_time ( ca len -- )
current_page file_mtime set_file_mtime ;
\ Set the modification time of the given file
\ to the correspondent metadata of the current page.
\ The host operating system shell is used.
: set_current_target_modification_time ( -- )
current_page target_path/file set_modification_time ;
\ Set the modification time of the current target file
\ to the correspondent metadata of the source file.
\ The host operating system shell is used.
: (close_target) ( -- )
\ depth abort" stack not empty" \ XXX INFORMER
target_fid @ close-file throw
\ ." target_fid just closed. " \ XXX INFORMER
target_fid off set_current_target_modification_time ;
\ Close the target file (HTML or Atom).
: close_target ( -- )
\ ." close_target" cr \ XXX INFORMER
target_fid @ if (close_target) then ;
\ Close the target file (HTML or Atom), if needed.
\ ==============================================================
\ Files {{{1
: file>local ( ca1 len1 -- ca2 len2 )
2>r target_dir $@ files_subdir $@ s+ 2r> s+ ;
\ Add local path to filename _ca1 len1_, resulting
\ _ca2 len2_.
\ ==============================================================
\ Redirection {{{1
: ((redirected)) ( fid -- )
>r s" <?php" r@ write-line throw
s" header('HTTP/1.1 301 Moved Permanently');" r@ write-line throw
s" header('Status: 301 Moved Permanently');" r@ write-line throw
S" header('Location: http://'."
s" ($_SERVER['HTTP_HOST']=='localhost'?'localhost/':'')" s+
s" .'" s+
domain¤t_target_file s+
s" ');" s+ r@ write-line throw
s" exit(0);" r@ write-line throw
s" ?>" r> write-line throw ;
\ Create the content of a file that redirects to the current page.
: ?+redirected_extension ( ca1 len1 -- ca2 len2 )
2dup s" .html" string-suffix? ?exit
2dup s" .php" string-suffix? ?exit
2dup s" .htm" string-suffix? ?exit
html_extension $@ s+ ;
\ Make sure _ca1 len1_, which is a page ID (old page filename
\ without path and extension) or a page filename (with html, htm or
\ php extensions), have a filename extension. If not, add the
\ default HTML extension.
: redirected>target ( ca1 len1 -- ca2 len2 )
?+redirected_extension +target_dir ;
\ Convert an old page ID (whose filename does not exist any more)
\ to its correspondent target filename.
\ The default target extension is assumed.
\ ca1 len1 = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
\ ca2 len2 = target filename with path
\ The file modification time of a redirected file can be
\ the modification or creation dates of the page they point to.
defer redirection_date ( a1 -- a2 )
: modified>redirection ( -- )
['] file_mtime is redirection_date ;
\ The file modification time of the next redirected file will
\ be the modification date of the current page.
\ The calculated datum `file_mtime` is used instead of `modified`,
\ because it gives more control to the user.
: created>redirection ( -- )
['] created is redirection_date ;
\ The file modification time of the next redirected file will
\ be the creation date of the current page.
\ The default is the `created` date. This way the redirected files
\ are not uploaded during server updates.
created>redirection
: (redirected) ( ca len -- )
redirected>target 2dup 2>r w/o create-file throw
dup ((redirected)) close-file throw
2r> current_page redirection_date set_file_mtime
created>redirection ; \ restore the default
\ Create a file that redirects to the current page.
\ ca len = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
\ 2013-10-02 Start, based on code from ForthCMS.
: redirected ( ca len -- )
current_page draft? if 2drop else (redirected) then ;
\ Create a file that redirects to the current page, if possible.
\ ca len = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
: redirect ( "name" -- )
parse-name redirected ;
\ Create a file that redirects to the current page, if possible.
\ "name" = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
: new_redirected ( ca len -- )
current_page draft? if 2drop
else modified>redirection (redirected)
created>redirection
then ;
\ Create a file that redirects to the current page, if possible.
\ The date of the redirection file will be the modification date
\ of the current page.
\ ca len = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
: new_redirect ( "name" -- )
parse-name new_redirected ;
\ Create a file that redirects to the current page, if possible.
\ The date of the redirection file will be the modification date
\ of the current page.
\ "name" = page ID (old page filename without path and extension)
\ or page filename (with html, htm or php extensions)
\ ==============================================================
\ Read source code {{{1
s" /counted-string" environment? 0=
[if] 255 [then] dup constant /source_line
2 chars + buffer: source_line
: read_fid_line { fid -- ca len f }
source_line dup /source_line fid read-line throw ;
\ Get a line from the given file identifier.
: read_source_line ( -- ca len f )
source-id read_fid_line ;
\ Get a line from the current source.
.( fendo.files.fs compiled ) cr
\ ==============================================================
\ Change log {{{1
\ 2013-10-01: File created for the page redirection tools;
\ `open_target` and `close_target` are moved here from
\ "fendo_parser.fs".
\
\ 2013-10-02: Page redirection tools.
\
\ 2013-11-18: New: `file>local`, factored from `open_source_code`,
\ (defined in <addons/source_code.fs>).
\
\ 2013-11-28: Fix: `redirected` didn't add the target extension, only
\ the path; fixed with the new word `pid$>target`.
\
\ 2014-03-02: Change: `domain¤t_target_file`, factored from
\ `(redirected)` to <fendo.data.fs>.
\
\ 2014-07-13: New: `set_current_target_modification_time`.
\
\ 2014-11-04: Improvement: `redirected` set the file modification time
\ of the redirected file with the correspondent metadadatum of its
\ goal page. This way server updates will be easier.
\ 'set_current_target_modification_time ' was factored out.
\
\ 2015-01-14: Improvement: `set_modification_time` uses the new
\ calculated datum `file_mtime`.
\
\ 2015-01-14: Fix: `redirected` created the redirected file even when
\ the current page is a draft.
\
\ 2015-01-14: Improvement: `set_modification_time` is factored out to
\ `set_file_mtime`, that receives also the desired date. This lets the
\ redirection files to be set to the date the original was created.
\ The words for redirection are updated accordingly.
\
\ 2015-01-31: Change: there were two related words called
\ `(redirected)`. Although this caused no problem but a warning
\ message during compilation, for clarity the deeper one has been
\ renamed to `((redirected))`.
\
\ 2015-02-11: Typos.
\
\ 2017-06-22: Update source style, layout and header.
\
\ 2017-06-24: Improve `redirected>target`: add the HTML filename
\ extension only when the input string has no recognized extension.
\ Improve `set_file_mtime`: complete the ISO date if needed. Improve
\ comment.
\
\ 2017-06-25: Improve `set_file_mtime`: do nothing if the date string
\ is empty.
\
\ 2018-12-08: Update notation of Forth words in comments and strings.
\
\ 2018-12-08: Update notation of page IDs in comments and strings.
\
\ 2020-04-22: Fix/improve `new_redirect` and `new_redirected`.
\ vim: filetype=gforth