Module: Coradoc::Introspection

Defined in:
lib/coradoc/introspection.rb,
lib/coradoc/introspection/element_counter.rb

Overview

Document introspection: file metadata, validation, stats. Extracted from the top-level Coradoc façade so the document-counting visitor and file-metadata helpers have their own home. Public API on Coradoc delegates here.

Defined Under Namespace

Classes: ElementCounter

Class Method Summary collapse

Class Method Details

.describe_element(elem) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coradoc/introspection.rb', line 40

def describe_element(elem)
  return elem.to_s unless elem.is_a?(CoreModel::Base)

  type = elem.class.name.split('::').last
  if elem.title
    "#{type}: #{elem.title}"
  elsif elem.is_a?(CoreModel::Block) && elem.content
    preview = elem.content.to_s[0..50]
    preview += '...' if elem.content.to_s.length > 50
    "#{type}: #{preview}"
  else
    type
  end
end

.document_stats(doc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coradoc/introspection.rb', line 28

def document_stats(doc)
  stats = {}
  stats[:title] = doc.title if doc.title

  if doc.is_a?(CoreModel::StructuralElement)
    stats[:child_count] = count_elements(doc)
    stats[:element_counts] = count_element_types(doc)
  end

  stats
end

.file_info(path) ⇒ Object



12
13
14
15
16
17
# File 'lib/coradoc/introspection.rb', line 12

def file_info(path)
  fmt = FormatCatalog.detect_format(path)
  info = { size: File.size(path), format: fmt }
  info[:lines] = File.foreach(path).count unless FormatCatalog.binary_format?(fmt)
  info
end

.validate_file(path, format: nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/coradoc/introspection.rb', line 19

def validate_file(path, format: nil)
  doc = Pipeline.parse_file(path, format: format)

  schema = Validation::SchemaGenerator.generate(doc.class)
  return schema.validate(doc) if schema

  Validation::Result.new
end