fdoc

subroutinecollect_definitionsprivate

subroutine collect_definitions(t, lines)

Remove link references, footnotes and abbreviations from lines and keep them.

Arguments

NameTypeIntentAttributesDescription
ttype(tree_t)inout

the conversion

linestype(string_t)inoutallocatabledimension(:)

the lines

Calls

Source

Show the code
subroutine collect_definitions(t, lines)
    type(tree_t), intent(inout) :: t                        !! the conversion
    type(string_t), allocatable, intent(inout) :: lines(:)  !! the lines
    type(string_t), allocatable :: out(:)
    character(len=:), allocatable :: id, value, url, title, body
    type(buffer_t) :: b
    integer :: k, close, at, j

    allocate (out(0))
    k = 1
    do while (k <= size(lines))
        associate (line => lines(k)%s)
            close = 0
            at = verify(line, ' ')
            if (at >= 1 .and. at <= 4) then
                if (line(at:at) == '[') close = index(line(at:), ']:') + at - 1
                if (close > at + 0 .and. index(line(at:close - 1), ']') > 0) close = 0
                if (close == at - 1) close = 0
            end if
            if (at == 1 .and. starts_with(line, '*[')) then
                j = index(line, ']:')
                if (j > 3) then
                    t%abbreviations = [t%abbreviations, map_entry_t(line(3:j - 1), strip(line(j + 2:)))]
                    k = k + 1
                    cycle
                end if
            end if
            if (close == 0) then
                call push(out, line)
                k = k + 1
                cycle
            end if
            id = line(at + 1:close - 1)
            value = strip(line(close + 2:))
        end associate
        if (starts_with(id, '^')) then
            ! a footnote: the rest of the line, and the indented lines and blocks after it
            call add(b, value)
            call add(b, NL)
            k = k + 1
            do while (k <= size(lines))
                if (starts_with(lines(k)%s, '    ')) then
                    call add(b, lines(k)%s(5:))
                    call add(b, NL)
                else if (len(lines(k)%s) == 0 .and. k < size(lines)) then
                    if (.not. starts_with(lines(k + 1)%s, '    ')) exit
                    call add(b, NL)
                else
                    exit
                end if
                k = k + 1
            end do
            call take(b, body)
            t%notes = [t%notes, footnote_t(id(2:), body, .false.)]
            cycle
        end if
        if (len(value) == 0 .and. k < size(lines)) then
            k = k + 1
            value = strip(lines(k)%s)
        end if
        if (len(value) == 0) then
            call push(out, lines(k)%s)
            k = k + 1
            cycle
        end if
        title = ''
        at = scan(value, ' ')
        if (at > 0) then
            url = value(:at - 1)
            title = strip(value(at + 1:))
            if (len(title) >= 2 .and. scan(title(1:1), '"''(') > 0) then
                title = title(2:len(title) - 1)
            else
                call push(out, lines(k)%s)
                k = k + 1
                cycle
            end if
        else
            url = value
        end if
        if (starts_with(url, '<') .and. ends_with(url, '>')) url = url(2:len(url) - 1)
        call normal_id(id, value)
        t%refs = [t%refs, reference_t(value, url, title)]
        k = k + 1
        if (k <= size(lines) .and. len(title) == 0) then
            value = strip(lines(k)%s)
            if (len(value) >= 2) then
                if (scan(value(1:1), '"''(') > 0 .and. scan(value(len(value):len(value)), '"'')') > 0) then
                    t%refs(size(t%refs))%title = value(2:len(value) - 1)
                    k = k + 1
                end if
            end if
        end if
    end do
    call move_alloc(out, lines)
end subroutine collect_definitions

Graphs

Calls

Called by