subroutinestash_html_blocksrecursiveprivate
recursive subroutine stash_html_blocks(t, lines, options)
Replace block-level HTML by placeholders, rendering the contents of markdown="1" elements.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| t | type(tree_t) | inout | the conversion | |
| lines | type(string_t) | inout | allocatabledimension(:) | the lines |
| options | type(markdown_options_t) | in | what rendering depends on |
Calls
- tag_at
- starts_with
- push
- join
- element_end
- remove_markdown_attribute
- markdown_to_html
- add
- take
- stash_html
- count_newlines
- strip
- first_line_of
Source
Show the code
recursive subroutine stash_html_blocks(t, lines, options)
type(tree_t), intent(inout) :: t !! the conversion
type(string_t), allocatable, intent(inout) :: lines(:) !! the lines
type(markdown_options_t), intent(in) :: options !! what rendering depends on
type(string_t), allocatable :: out(:)
character(len=:), allocatable :: tag, text, placeholder, open_tag, inner, rendered, rest
type(buffer_t) :: b
integer :: k, at, stop_at, j, gt
logical :: previous_blank
if (.not. any([(index(lines(k)%s, '<') > 0, k=1, size(lines))])) return
allocate (out(0))
previous_blank = .true.
k = 1
do while (k <= size(lines))
at = verify(lines(k)%s, ' ')
tag = ''
if (at > 0 .and. at <= 4 .and. previous_blank) then
if (lines(k)%s(at:at) == '<') then
call tag_at(lines(k)%s, at, tag)
if (index(BLOCK_TAGS, ' '//tag//' ') == 0) tag = ''
if (starts_with(lines(k)%s(at:), '<!--')) tag = '!--'
end if
end if
if (len(tag) == 0) then
call push(out, lines(k)%s)
previous_blank = len(lines(k)%s) == 0
k = k + 1
cycle
end if
text = join(lines(k:), NL)
call element_end(text, at, tag, stop_at)
if (stop_at == 0) then
call push(out, lines(k)%s)
previous_blank = .false.
k = k + 1
cycle
end if
gt = index(text(at:), '>') + at - 1
open_tag = text(at:gt)
if (index(lowercase(open_tag), 'markdown=') > 0 .and. tag /= '!--') then
call remove_markdown_attribute(open_tag)
inner = text(gt + 1:stop_at - len(tag) - 3)
call markdown_to_html(inner, options, rendered)
call add(b, open_tag//NL)
call add(b, rendered)
call add(b, NL//'</'//tag//'>')
call take(b, rendered)
call stash_html(t, rendered, placeholder)
else
call stash_html(t, text(at:stop_at), placeholder)
end if
call push(out, '')
call push(out, placeholder)
call push(out, '')
! carry on after the element
j = 1 + count_newlines(text(:stop_at))
rest = text(stop_at + 1:)
k = k + j
if (len(strip(first_line_of(rest))) > 0) then
k = k - 1
lines(k)%s = first_line_of(rest)
end if
previous_blank = .true.
end do
call move_alloc(out, lines)
end subroutine stash_html_blocks