Class: Glossarist::ConceptSet

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/concept_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concepts, assets, options = {}) ⇒ ConceptSet

Returns a new instance of ConceptSet.



23
24
25
26
27
28
29
30
31
# File 'lib/glossarist/concept_set.rb', line 23

def initialize(concepts, assets, options = {})
  @concepts = read_concepts(concepts)
  @assets = Glossarist::Collections::AssetCollection.new(assets)
  @bibliographies = Glossarist::Collections::BibliographyCollection.new(
    @concepts,
    options.dig(:bibliography, :global_cache),
    options.dig(:bibliography, :local_cache),
  )
end

Instance Attribute Details

#assetsObject

an Collections::Asset object



16
17
18
# File 'lib/glossarist/concept_set.rb', line 16

def assets
  @assets
end

#bibliographiesObject

a BibliographyCollection object



13
14
15
# File 'lib/glossarist/concept_set.rb', line 13

def bibliographies
  @bibliographies
end

#conceptsObject

An Enumerable of ManagedConcept (Array, GlossaryStore, or ManagedConceptCollection). Set by read_concepts based on what the caller passed.



10
11
12
# File 'lib/glossarist/concept_set.rb', line 10

def concepts
  @concepts
end

Instance Method Details

#concept_mapObject



85
86
87
88
89
# File 'lib/glossarist/concept_set.rb', line 85

def concept_map
  @concept_map ||= concepts.to_a.to_h do |concept|
    [concept.default_designation.downcase, concept]
  end
end

#latex_template(concept) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/glossarist/concept_set.rb', line 65

def latex_template(concept)
  <<~TEMPLATE
    \\newglossaryentry{#{concept.default_designation.tr('_', '-')}}
    {
    name={#{concept.default_designation.gsub('_', '\_')}}
    description={#{normalize_definition(concept.default_definition)}}
    }
  TEMPLATE
end

#normalize_definition(definition) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/glossarist/concept_set.rb', line 75

def normalize_definition(definition)
  definition.gsub(/{{([^}]*)}}/) do |_match|
    inner = Regexp.last_match[1]
    # Mention syntax: {{identifier}} or {{identifier, render term}}
    # Use the identifier (first part before comma) as the gloss label.
    label = inner.split(",", 2).first.strip.tr("_", "-")
    "\\textbf{\\gls{#{label}}}"
  end
end

#read_concepts(concepts) ⇒ Object

Loads concepts via GlossaryStore when given a path string. Accepts an existing Enumerable (Array, GlossaryStore, ManagedConceptCollection) as-is — callers that already have an in-memory collection can pass it directly without paying for a re-load.



57
58
59
60
61
62
63
# File 'lib/glossarist/concept_set.rb', line 57

def read_concepts(concepts)
  return concepts if concepts.is_a?(Enumerable)

  store = GlossaryStore.new
  store.load(concepts)
  store.concepts
end

#to_latex(filename = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/glossarist/concept_set.rb', line 33

def to_latex(filename = nil)
  return to_latex_from_file(filename) if filename

  concepts.to_a.map do |concept|
    latex_template(concept)
  end.join("\n")
end

#to_latex_from_file(entries_file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/glossarist/concept_set.rb', line 41

def to_latex_from_file(entries_file)
  File.readlines(entries_file).filter_map do |concept_name|
    concept = concept_map[concept_name.strip.downcase]

    if concept.nil?
      puts "  [Not Found]: #{concept_name.strip}"
    else
      latex_template(concept)
    end
  end.join("\n")
end