subroutineunique_idprivate
subroutine unique_id(t, html, id)
An id for a heading with text html, not used before. Like Python-Markdown's slugify, this drops
punctuation and turns each run of blanks and hyphens into one hyphen.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| t | type(tree_t) | inout | the conversion | |
| html | character(len=*) | in | the heading | |
| id | character(len=:) | out | allocatable | the id |
Calls
Source
Show the code
subroutine unique_id(t, html, id)
type(tree_t), intent(inout) :: t !! the conversion
character(len=*), intent(in) :: html !! the heading
character(len=:), allocatable, intent(out) :: id !! the id
type(buffer_t) :: b
character(len=:), allocatable :: base
character(len=12) :: number
logical :: in_tag, separated, hyphen
integer :: k, n, semicolon
in_tag = .false.
separated = .false.
hyphen = .false.
k = 0
do while (k < len(html))
k = k + 1
associate (c => html(k:k))
if (c == '<') in_tag = .true.
if (in_tag) then
if (c == '>') in_tag = .false.
cycle
end if
if (c == '&') then
semicolon = index(html(k:min(k + 9, len(html))), ';')
if (semicolon > 0) k = k + semicolon - 1
else if (c == '-' .or. c == ' ' .or. c == achar(9) .or. c == achar(10)) then
separated = .true.
hyphen = hyphen .or. c == '-'
else if (index(WORD_CHARS, c) > 0) then
if (separated .and. (b%n > 0 .or. hyphen)) call add(b, '-')
separated = .false.
hyphen = .false.
call add(b, lower_char(c))
end if
end associate
end do
if (hyphen) call add(b, '-')
call take(b, base)
id = base
n = 0
do while (contains_string(t%ids, id) .or. len(id) == 0)
n = n + 1
number = int_text(n)
id = base//'_'//trim(number)
end do
call push(t%ids, id)
end subroutine unique_id