Class: TextLine

Inherits:
TextLineBuilderContext show all
Defined in:
lib/almirah/doc_items/text_line.rb

Direct Known Subclasses

ControlledTableColumn, DocItem

Constant Summary collapse

nil
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

Cross-document links that could not be resolved to a managed document, collected during rendering for reporting (ADR-186, SRS-094).



401
402
403
# File 'lib/almirah/doc_items/text_line.rb', line 401

def broken_links
  @@broken_links
end


395
396
397
# File 'lib/almirah/doc_items/text_line.rb', line 395

def link_registry
  @@link_registry
end


391
392
393
# File 'lib/almirah/doc_items/text_line.rb', line 391

def link_registry=(registry)
  @@link_registry = registry # rubocop:disable Style/ClassVars
end


409
410
411
# File 'lib/almirah/doc_items/text_line.rb', line 409

def record_broken_link(document, target)
  @@broken_links << { document: document&.id, target: target }
end


405
406
407
# File 'lib/almirah/doc_items/text_line.rb', line 405

def reset_broken_links
  @@broken_links = [] # rubocop:disable Style/ClassVars
end

Instance Method Details

#bold(str) ⇒ Object



430
431
432
# File 'lib/almirah/doc_items/text_line.rb', line 430

def bold(str)
  "<b>#{str}</b>"
end

#bold_and_italic(str) ⇒ Object



434
435
436
# File 'lib/almirah/doc_items/text_line.rb', line 434

def bold_and_italic(str)
  "<b><i>#{str}</i></b>"
end

#format_string(str) ⇒ Object



420
421
422
423
424
# File 'lib/almirah/doc_items/text_line.rb', line 420

def format_string(str)
  tlp = TextLineParser.new
  tlb = TextLineBuilder.new(self)
  tlb.restore(tlp.tokenize(str))
end

#inline_code(str) ⇒ Object



438
439
440
# File 'lib/almirah/doc_items/text_line.rb', line 438

def inline_code(str)
  "<code class=\"inline\">#{CGI.escapeHTML(str)}</code>"
end

#italic(str) ⇒ Object



426
427
428
# File 'lib/almirah/doc_items/text_line.rb', line 426

def italic(str)
  "<i>#{str}</i>"
end

rubocop:disable Metrics/MethodLength



442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/almirah/doc_items/text_line.rb', line 442

def link(link_text, link_url) # rubocop:disable Metrics/MethodLength
  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=\"#{href}\" class=\"external\">#{link_text}</a>"
  when :broken
    TextLine.record_broken_link(owner_document, raw)
    "<a href=\"#{raw}\" class=\"broken_link\" title=\"Unresolved cross-document link\">#{link_text}</a>"
  else
    "<a target=\"_blank\" rel=\"noopener\" href=\"#{raw}\" class=\"external\">#{link_text}</a>"
  end
end

#owner_documentObject

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).



416
417
418
# File 'lib/almirah/doc_items/text_line.rb', line 416

def owner_document
  nil
end

Resolves an Obsidian/wiki link “[[target#fragment|alias]]” to a managed document by its unique id/filename, independent of folder (ADR-186).



459
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 459

def wiki_link(inner) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/AbcSize,Metrics/MethodLength
  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