aboutsummaryrefslogtreecommitdiff
path: root/captain/template.f90
blob: 3ae3daebc876ab5549af5e3eeb51449e362eb8dc (plain)
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
! Copyright (c) 2021 Approximatrix, LLC <support@approximatrix.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module page_template
implicit none

    integer, parameter::VTYPE_NONE = 0, &
                        VTYPE_STRING = 1, &
                        VTYPE_INTEGER = 2, &
                        VTYPE_LOGICAL = 3

    type :: variable
    
        character(len=:), pointer::vname
        
        integer::vtype
        character(len=:), pointer::vstr
        integer::vint
        
        contains
        
        procedure :: destroy => variable_destroy
        procedure :: shallow_copy => variable_shallow_copy
        procedure :: set_name => variable_set_name
        procedure :: set_string_value => variable_set_string_value
        procedure :: assign_integer => variable_assign_integer
        procedure :: assign_string => variable_assign_string
        procedure :: assign_logical => variable_assign_logical
        
        generic :: assign => assign_integer, assign_string, assign_logical
    end type
    
    type :: template
    
        character(len=:), pointer::base_filename
        type(variable), dimension(:), pointer::variables
        character(len=:), pointer::output_filename
        
        contains
        
        procedure :: init => template_init
        procedure :: destroy => template_destroy
        procedure :: assign_integer => template_assign_integer
        procedure :: assign_string => template_assign_string
        procedure :: assign_logical => template_assign_logical
        procedure :: render => template_render
        procedure :: evaluate => template_evaluate
        procedure :: generate_output_filename => template_generate_output_filename
        
        generic :: assign => assign_integer, assign_string, assign_logical
    end type

contains

    elemental subroutine variable_destroy(self)
    implicit none
    
        class(variable), intent(inout)::self
        
        if(associated(self%vname)) then
            deallocate(self%vname)
        end if
        
        if(associated(self%vstr)) then
            deallocate(self%vstr)
        end if
        
    end subroutine variable_destroy

    subroutine variable_shallow_copy(self, v)
    implicit none
    
        class(variable), intent(inout)::self
        class(variable), intent(in)::v

        if(associated(v%vname)) then
            self%vname => v%vname
        end if
        
        if(associated(v%vstr)) then
            self%vstr => v%vstr
        end if
            
        self%vtype = v%vtype
        self%vint = v%vint
        
    end subroutine variable_shallow_copy

    subroutine variable_set_name(self, name)
    use logging
    implicit none
    
        class(variable)::self
        character(*), intent(in)::name

        allocate(character(len=max(len_trim(name), 1)) :: self%vname)
        if(len_trim(name) == 0) then
            self%vname = " "
        else
            self%vname = trim(name)
        end if
        
    end subroutine variable_set_name
        
    subroutine variable_set_string_value(self, v)
    implicit none
    
        class(variable)::self
        character(*), intent(in)::v
        
        allocate(character(len=max(len_trim(v), 1)) :: self%vstr)
        if(len_trim(v) == 0) then
            self%vstr = " "
        else
            self%vstr = trim(v)
        end if
        
    end subroutine variable_set_string_value

    subroutine variable_assign_integer(self, name, i)
    implicit none
        
        class(variable)::self
        character(*), intent(in)::name
        integer, intent(in)::i
        character(16)::int_string
        
        call self%set_name(name)
        self%vint = i
        self%vtype = VTYPE_INTEGER
        
        write(int_string, *) i
        
        call self%set_string_value(trim(adjustl(int_string)))
        
    end subroutine variable_assign_integer
    
    subroutine variable_assign_string(self, name, str)
    implicit none
    
        class(variable)::self
        character(*), intent(in)::name, str
        
        call self%set_name(name)        
        self%vtype = VTYPE_STRING
        call self%set_string_value(str)
        
    end subroutine variable_assign_string
    
    subroutine variable_assign_logical(self, name, lg)
    implicit none

        class(variable)::self
        character(*), intent(in)::name
        logical, intent(in)::lg
        
        call self%set_name(name)
        self%vtype = VTYPE_LOGICAL
        if(lg) then
            self%vint = 1
            call self%set_string_value("True")
        else
            self%vint = 0
            call self%set_string_value("False")
        end if

    end subroutine variable_assign_logical

    subroutine template_init(self, filename)
    implicit none
    
        class(template)::self
        character(*), intent(in)::filename
        
        allocate(character(len=len_trim(filename)) :: self%base_filename)
        self%base_filename = filename
        
        allocate(self%variables(32))
        self%variables%vtype = VTYPE_NONE
        
        self%output_filename => null()
        
    end subroutine template_init
    
    subroutine template_destroy(self)
    implicit none
    
        class(template)::self
        
        deallocate(self%base_filename)
        
        call self%variables%destroy()
        deallocate(self%variables)
        
    end subroutine template_destroy
    
    subroutine template_generate_output_filename(self)
    use utilities, only: generate_temporary_filename
    use logging
    implicit none
    
        class(template)::self
    
        self%output_filename => generate_temporary_filename()
        
    end subroutine template_generate_output_filename
    
    function template_available_variable_index(self) result(i)
    implicit none
    
        class(template)::self
        type(variable), dimension(:), pointer::tmp
        integer::i, j
        
        do i = 1, size(self%variables)
            if(self%variables(i)%vtype == VTYPE_NONE) then
                exit
            end if
        end do
        
        ! Need to expand
        if(i > size(self%variables)) then
            allocate(tmp(size(self%variables)+32))
            
            tmp%vtype = VTYPE_NONE
            
            do j=1, i-1
                call tmp(j)%shallow_copy(self%variables(j))
            end do
            
            ! We performed a shallow copy above, so don't destroy
            ! the old variables
            deallocate(self%variables)
            self%variables => tmp
            tmp => null()
        end if
        
    end function template_available_variable_index
    
    subroutine template_assign_integer(self, name, value)
    implicit none
    
        class(template)::self
        character(*), intent(in)::name
        integer, intent(in)::value
        integer::i
        
        i = template_available_variable_index(self)
        call self%variables(i)%assign(name, value)
        
    end subroutine template_assign_integer

    subroutine template_assign_string(self, name, value)
    use logging
    implicit none
    
        class(template)::self
        character(*), intent(in)::name, value
        integer::i

        i = template_available_variable_index(self)

        call self%variables(i)%assign(name, value)
        
        !call write_log(name//"=|||"//trim(value)//"|||", LOG_INFO)
        
    end subroutine template_assign_string

    subroutine template_assign_logical(self, name, value)
    implicit none
    
        class(template)::self
        character(*), intent(in)::name
        logical, intent(in)::value
        integer::i
        
        i = template_available_variable_index(self)
        
        call self%variables(i)%assign(name, value)
        
    end subroutine template_assign_logical
    
    function handle_template_extends(in_unit, extends_command) result(outfile)
    use utilities, only: generate_temporary_filename
    use config, only: template_filepath
    implicit none
    
        integer, intent(in)::in_unit
        character(*), intent(in)::extends_command
        
        character(len=:), pointer::outfile
        
        integer::base_unit, out_unit
        character(1024)::base_filename
        
        character(256)::variable, varbuffer
        character::this_char, last_char
        
        integer::i, istat
        
        i = index(extends_command, "as")
        if(i > 0) then
            call template_filepath(trim(adjustl(extends_command(9:i-1))), base_filename)
            variable = adjustl(extends_command(i+3:len_trim(extends_command)))
        
            open(newunit=base_unit, file=trim(base_filename), status="old", &
                 form="unformatted", access="stream")
                 
            outfile => generate_temporary_filename()
            open(newunit=out_unit, file=outfile, status="new", &
                 form="formatted", access="stream", action="write")
            
            last_char = ' '
            read(base_unit, iostat=istat) this_char
            do while(istat == 0)
                
                if(this_char == '{' .and. last_char == '{') then
                    
                    varbuffer = '{{'
                    
                    i = 2
                    do while(varbuffer(i-1:i) /= '}}' .and. istat == 0)
                        i = i + 1
                        read(base_unit, iostat=istat) varbuffer(i:i)
                    end do
                    
                    ! Found our variable!
                    if( trim(variable) == trim(adjustl(varbuffer(4:i-3))) ) then
                    
                        ! Write out the original input unit
                        read(in_unit, iostat=istat) this_char
                        do while(istat == 0)
                            write(out_unit, '(A1)',  advance='no') this_char
                            read(in_unit, iostat=istat) this_char
                        end do
                        
                    ! Not our variable
                    else
                        
                        do i = 1, len_trim(varbuffer)
                            write(out_unit, '(A1)',  advance='no') varbuffer(i:i)
                        end do
                        
                    end if
                
                    last_char = ' '
                    this_char = ' '
                else if(last_char == '{') then
                
                    write(out_unit, '(A1, A1)',  advance='no') last_char, this_char
                
                else if(this_char /= '{') then
                
                    write(out_unit, '(A1)',  advance='no') this_char
                
                end if
                
                last_char = this_char
                read(base_unit, iostat=istat) this_char
            end do
        
            close(base_unit)
            close(out_unit)
        
        end if
    
    end function handle_template_extends
    
    recursive subroutine template_render(self, filename, input_tempfile)
    use logging
    use utilities, only: delete_file
    implicit none
    
        class(template)::self
        character(*), intent(in), optional::filename, input_tempfile
        
        character::this_char, last_char
        character(len=2)::running_pair, closing_pair
        character(256)::brace_internals
        character(len=:), pointer::replacement
        
        logical::writing_now
        
        integer::input, istat, i, unum

        if(present(filename)) then
            allocate(character(len=len_trim(filename)) :: self%output_filename)
            self%output_filename = trim(filename)
        else if(.not. associated(self%output_filename)) then
            call self%generate_output_filename()
        end if

        open(newunit=unum, file=trim(self%output_filename), status="unknown", form="formatted", access="stream", action="write")
        
        ! We may be working with a temporary file after a 'extends' tag
        if(present(input_tempfile)) then
            open(newunit=input, file=trim(input_tempfile), status="old", form="unformatted", access="stream")
        else
            open(newunit=input, file=trim(self%base_filename), status="old", form="unformatted", access="stream")
        end if
        
        writing_now = .true.

        last_char = ' '
        read(input, iostat=istat) this_char
        do while(istat == 0)
        
            running_pair = last_char//this_char
            
            if(running_pair == '{{') then
            
                ! Future expansion
                if(running_pair == '{{') then
                    closing_pair = '}}'
                end if
        
                brace_internals = ' '
            
                read(input, iostat=istat) this_char
                i = 1
                do while(istat == 0 .and. (running_pair /= closing_pair))
                    brace_internals(i:i) = this_char
                    
                    i = i + 1
                    last_char = this_char
                    read(input, iostat=istat) this_char
                    running_pair = last_char//this_char
                end do
                brace_internals(i-1:i-1) = ' ' ! Remove trailing bracket
                brace_internals = adjustl(brace_internals)
                
                ! Simple variable
                if(closing_pair == '}}') then
                    replacement => self%evaluate(brace_internals)
                    
                    if(associated(replacement)) then
                        do i=1, len_trim(replacement)
                            write(unum, '(A1)',  advance='no') replacement(i:i)
                        end do
                        replacement => null() ! do not free - internal strings
                    end if
                    
                ! Expression
                else
                
                    
                end if
                
            else if(running_pair == '{%') then
            
                closing_pair = '%}'
                brace_internals = ' '
                
                read(input, iostat=istat) this_char
                i = 1
                do while(istat == 0 .and. (running_pair /= closing_pair))
                    brace_internals(i:i) = this_char
                    
                    i = i + 1
                    last_char = this_char
                    read(input, iostat=istat) this_char
                    running_pair = last_char//this_char
                end do
                brace_internals(i-1:i-1) = ' ' ! Remove trailing bracket
                brace_internals = adjustl(brace_internals)

                ! Parse the command
                if(brace_internals(1:8) == 'extends ') then
                
                    ! Replacement will be a new file to parse through this engine
                    replacement => handle_template_extends(input ,trim(brace_internals))
                    
                    ! Close the current set of files before retrying the rendering
                    close(unum)
                    close(input)
                    
                    ! Re-render using the constructed tempfile
                    if(associated(replacement)) then
                        call write_log("replacement template: "//trim(replacement), LOG_DEBUG)
                        call template_render(self, input_tempfile=replacement)
                        call write_log("template output: "//trim(self%output_filename), LOG_DEBUG)
                        ! It was temporary
                        call delete_file(replacement)
                        deallocate(replacement)
                    end if
                    
                    ! Stop rendering - it was handled above
                    return
                    
                end if
                
            else if(last_char == '{') then
                write(unum, '(A1)', advance='no') last_char
                write(unum, '(A1)', advance='no') this_char
                
            else if(this_char /= '{') then
                 write(unum, '(A1)', advance='no') this_char
            end if
        
            last_char = this_char
            read(input, iostat=istat) this_char
        end do
        
        close(unum)
        close(input)

    end subroutine template_render
    
    function template_evaluate(self, txt) result(res)
    implicit none
        
        class(template)::self
        character(*), intent(in)::txt
        character(:), pointer::res
        
        integer::i
        
        res => null()
        
        ! Right now, we only support straight-up variables
        do i = 1, size(self%variables)
            if(self%variables(i)%vtype == VTYPE_NONE) then
                exit
            end if
            
            if(self%variables(i)%vname == trim(txt) .and. associated(self%variables(i)%vstr)) then
                res => self%variables(i)%vstr
                exit
            end if
        
        end do

    end function template_evaluate
    
end module page_template