Class: TextLine
Constant Summary
collapse
- @@link_registry =
rubocop:disable Style/ClassVars
nil
- @@broken_links =
rubocop:disable Style/ClassVars
[]
Constants included
from HtmlSafe
HtmlSafe::ALLOWED_URL_SCHEMES
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from HtmlSafe
#escape_attr, #escape_text, #safe_url
Class Method Details
.broken_links ⇒ Object
Cross-document links that could not be resolved to a managed document, collected during rendering for reporting (ADR-186, SRS-094).
414
415
416
|
# File 'lib/almirah/doc_items/text_line.rb', line 414
def broken_links
@@broken_links
end
|
.link_registry ⇒ Object
408
409
410
|
# File 'lib/almirah/doc_items/text_line.rb', line 408
def link_registry
@@link_registry
end
|
.link_registry=(registry) ⇒ Object
404
405
406
|
# File 'lib/almirah/doc_items/text_line.rb', line 404
def link_registry=(registry)
@@link_registry = registry end
|
.record_broken_link(document, target) ⇒ Object
422
423
424
|
# File 'lib/almirah/doc_items/text_line.rb', line 422
def record_broken_link(document, target)
@@broken_links << { document: document&.id, target: target }
end
|
.reset_broken_links ⇒ Object
418
419
420
|
# File 'lib/almirah/doc_items/text_line.rb', line 418
def reset_broken_links
@@broken_links = [] end
|
Instance Method Details
#bold(str) ⇒ Object
448
449
450
|
# File 'lib/almirah/doc_items/text_line.rb', line 448
def bold(str)
"<b>#{str}</b>"
end
|
#bold_and_italic(str) ⇒ Object
452
453
454
|
# File 'lib/almirah/doc_items/text_line.rb', line 452
def bold_and_italic(str)
"<b><i>#{str}</i></b>"
end
|
433
434
435
436
437
|
# File 'lib/almirah/doc_items/text_line.rb', line 433
def format_string(str)
tlp = TextLineParser.new
tlb = TextLineBuilder.new(self)
tlb.restore(tlp.tokenize(str))
end
|
#inline_code(str) ⇒ Object
456
457
458
|
# File 'lib/almirah/doc_items/text_line.rb', line 456
def inline_code(str)
"<code class=\"inline\">#{CGI.escapeHTML(str)}</code>"
end
|
#italic(str) ⇒ Object
444
445
446
|
# File 'lib/almirah/doc_items/text_line.rb', line 444
def italic(str)
"<i>#{str}</i>"
end
|
#link(link_text, link_url) ⇒ Object
rubocop:disable Metrics/MethodLength
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/almirah/doc_items/text_line.rb', line 460
def link(link_text, link_url) raw = link_url.to_s
kind, target, fragment = classify_markdown_link(raw)
case kind
when :internal
href = RelativeUrl.between(owner_document.output_rel_path, target.output_rel_path, fragment: fragment)
"<a href=\"#{escape_attr(href)}\" class=\"external\">#{link_text}</a>"
when :broken
TextLine.record_broken_link(owner_document, raw)
"<a href=\"#{escape_attr(raw)}\" class=\"broken_link\" title=\"Unresolved cross-document link\">#{link_text}</a>"
else
url = safe_url(raw)
return link_text if url.nil?
"<a target=\"_blank\" rel=\"noopener\" href=\"#{escape_attr(url)}\" class=\"external\">#{link_text}</a>"
end
end
|
#literal_text(str) ⇒ Object
Literal text run, HTML-escaped for element content (ADR-188, SRS-096).
440
441
442
|
# File 'lib/almirah/doc_items/text_line.rb', line 440
def literal_text(str)
escape_text(str)
end
|
#owner_document ⇒ Object
The document that owns this text line. Used to resolve cross-document links relative to the current page. nil for stand-alone text (e.g. unit tests).
429
430
431
|
# File 'lib/almirah/doc_items/text_line.rb', line 429
def owner_document
nil
end
|
#wiki_link(inner) ⇒ Object
Resolves an Obsidian/wiki link “[[target#fragment|alias]]” to a managed document by its unique id/filename, independent of folder (ADR-186).
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
# File 'lib/almirah/doc_items/text_line.rb', line 480
def wiki_link(inner) link_part, sep, alias_text = inner.partition('|')
target, _hash, fragment = link_part.partition('#')
display = (sep.empty? ? link_part : alias_text).strip
display = link_part.strip if display.empty?
doc = TextLine.link_registry&.find_by_id(target.strip)
if doc && owner_document&.output_rel_path
href = RelativeUrl.between(owner_document.output_rel_path, doc.output_rel_path,
fragment: fragment.strip.empty? ? nil : fragment.strip)
"<a href=\"#{href}\" class=\"external\">#{CGI.escapeHTML(display)}</a>"
elsif owner_document&.output_rel_path
TextLine.record_broken_link(owner_document, "[[#{inner}]]")
"<span class=\"broken_link\" title=\"Unresolved wiki link\">#{CGI.escapeHTML(display)}</span>"
else
"[[#{inner}]]"
end
end
|