Class: HardCiter::HtmlOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/hardciter/html_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csl = nil) ⇒ HtmlOutput

Returns a new instance of HtmlOutput.



7
8
9
10
11
12
13
# File 'lib/hardciter/html_output.rb', line 7

def initialize(csl=nil)
  @csl = csl if csl
  @open_tag = '<sup>['
  @close_tag = ']</sup>'
  @multi_separator = ','
  @separator_after_last = false
end

Instance Attribute Details

#bibliographyObject

Returns the value of attribute bibliography.



5
6
7
# File 'lib/hardciter/html_output.rb', line 5

def bibliography
  @bibliography
end

#cslObject

Returns the value of attribute csl.



5
6
7
# File 'lib/hardciter/html_output.rb', line 5

def csl
  @csl
end

Instance Method Details

#check_text_and_matches_length(text, intext_matches) ⇒ Object



36
37
38
39
40
# File 'lib/hardciter/html_output.rb', line 36

def check_text_and_matches_length(text,intext_matches)
  if text.length != intext_matches.length
    raise "Document length and intext_matches length are different, aborting."
  end
end

#get_intext(citation) ⇒ Object



123
124
125
126
# File 'lib/hardciter/html_output.rb', line 123

def get_intext(citation)
  number = citation.bib_number
  "<a href = \"#bibliography_#{number}\">#{number}</a>"
end

#multi_cite(match, line, pos_offset) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hardciter/html_output.rb', line 79

def multi_cite(match, line, pos_offset)
  original_line_length = line.length
  pos = match.position + pos_offset
  before,after = split_at_match(pos,match.regex_match.length,line)
  after = split_group_matches(match.next_in_group,after)
  intexts = []
  while match do
    intexts.push(get_intext(match.citation))
    match = match.next_in_group
  end
  output_line = before + wrap_intext(intexts) + after
  pos_offset += (output_line.length - original_line_length)
  return output_line, pos_offset
end

#output_line(line, intext_matches) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/hardciter/html_output.rb', line 15

def output_line(line,intext_matches)
  intext_matches.each do |match|
    line_front = line[0..match.position-1]
    line_end = line[match.position+match.regex_match.length..-1]
    intext_out = @open_tag + match.citation.intext_output + @close_tag
    line = line_front + intext_out + line_end
  end
  line
end

#prepare_bibliography(bibliography) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hardciter/html_output.rb', line 42

def prepare_bibliography(bibliography)
  @bibliography = ['<ol class="bibliography">']
  bibliography.citations.each do |citation_key,citation|
    entry = citation.entry
    if entry.nil?
      cite_text = citation_key
    else
      cite_text = CiteProc.process(entry.to_citeproc, style: @csl, format: :html)
      strip_extra_brackets(cite_text)
      @bibliography.push '<li><a name = "' +
                     "bibliography_#{citation.bib_number}\">" +
                     cite_text + '</a></li>'
    end
  end
  @bibliography.push('</ol>')
end

#process_and_output_text(text, intext_matches) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/hardciter/html_output.rb', line 25

def process_and_output_text(text,intext_matches)
  text = text.text_array
  raise "prepare_bibliography has not been run, cannot process text" if @bibliography.nil?
  check_text_and_matches_length(text,intext_matches)
  output = []
  text.each_with_index do |line,index|
    output+=style_line(line,intext_matches[index])
  end
  output
end

#single_cite(match, line, pos_offset) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/hardciter/html_output.rb', line 113

def single_cite(match, line, pos_offset)
  original_line_length = line.length
  pos = match.position += pos_offset
  before,after = split_at_match(pos,match.regex_match.length, line)
  in_text_citation = wrap_intext([get_intext(match.citation)])
  output_line = before + in_text_citation + after
  pos_offset += (output_line.length - original_line_length)
  return output_line, pos_offset
end

#split_at_match(pos, match_length, line) ⇒ Object



107
108
109
110
111
# File 'lib/hardciter/html_output.rb', line 107

def split_at_match(pos,match_length,line)
  pos == 0 ? before = '' : before = line.slice!(0..pos - 1)
  after = line.slice(match_length,line.length)
  return before,after
end

#split_group_matches(match, line) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hardciter/html_output.rb', line 94

def split_group_matches(match,line)
  while match do
    match_pos = Regexp.new(match.regex_match).match(line)
    if match_pos.nil?
      raise "match missing?"
    else
      line.slice!(match_pos.begin(0)..match.regex_match.length)
    end
    match = match.next_in_group
  end
  line
end

#strip_extra_brackets(line) ⇒ Object



59
60
61
# File 'lib/hardciter/html_output.rb', line 59

def strip_extra_brackets(line)
    line.gsub!(/\{|\}/, '')
end

#style_line(line, line_matches) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hardciter/html_output.rb', line 63

def style_line(line, line_matches)
  pos_offset = 0
  line_matches.each do |match|
    if match
      if match.type == BIBLIOGRAPHY_OUT_MATCH
       return @bibliography
      elsif match.next_in_group.nil?
        line,pos_offset = single_cite(match,line,pos_offset)
      else
        line,pos_offset = multi_cite(match, line, pos_offset)
      end
    end
  end
  return [line]
end

#wrap_intext(intext_citations) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/hardciter/html_output.rb', line 128

def wrap_intext(intext_citations)
  output = @open_tag
  intext_citations.each_with_index do |intext, index|
    output += intext
    output += @multi_separator unless index == intext_citations.length-1
  end
  output += @close_tag
end