Class: Nsf::Paragraph

Inherits:
Object show all
Defined in:
lib/nsf.rb,
lib/nsf/formats/nsf.rb,
lib/nsf/formats/pdf.rb,
lib/nsf/formats/rtf.rb,
lib/nsf/formats/html.rb

Direct Known Subclasses

Fixedblock

Constant Summary collapse

BOLD_ITALIC_REGEX =
/(?:(\W|^)(\*)|(\*)(\W|$)|(\W|^)(_)|(_)(\W|$))/
PDF_PARAGRAPH_LEADING =
1.2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Paragraph

Returns a new instance of Paragraph.



48
49
50
# File 'lib/nsf.rb', line 48

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



44
45
46
# File 'lib/nsf.rb', line 44

def text
  @text
end

Class Method Details

.from_nsf(text) ⇒ Object



52
53
54
# File 'lib/nsf.rb', line 52

def self.from_nsf(text)
  self.new(text.gsub(/[[:space:]]+/, ' ').strip)
end

Instance Method Details

#to_htmlObject



152
153
154
# File 'lib/nsf/formats/html.rb', line 152

def to_html
  "<p>#{to_html_fragment}</p>"
end

#to_html_fragment(escape = true) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nsf/formats/html.rb', line 129

def to_html_fragment(escape = true)
  out = (escape ? CGI.escapeHTML(@text) : @text).split(BOLD_ITALIC_REGEX)

  if ((out.select { |element| element == "*" }).length % 2 == 0) && ((out.select { |element| element == "_" }).length % 2 == 0)
    in_bold = false
    in_italic = false

    out.map! do |element|
      if element == "*"
        in_bold = !in_bold
        in_bold ? "<b>" : "</b>" #Note that in_bold has been inverted, so this is inverted as well
      elsif element == "_"
        in_italic = !in_italic
        in_italic ? "<i>" : "</i>" #Note that in_italic has been inverted, so this is inverted as well
      else
        element
      end
    end
  end

  out.join
end

#to_nsfObject



13
14
15
16
# File 'lib/nsf/formats/nsf.rb', line 13

def to_nsf
  @text
#      word_wrap(@text, 80)
end

#to_pdf(pdf) ⇒ Object



31
32
33
34
# File 'lib/nsf/formats/pdf.rb', line 31

def to_pdf(pdf)
  pdf.text to_html_fragment(false), :inline_format => true
  pdf.move_down (pdf.font_size * PDF_PARAGRAPH_LEADING).round
end

#to_rtfObject

RTF_ITALIC = RTF::CharacterStyle.new RTF_ITALIC.italic = true



49
50
51
52
53
54
55
# File 'lib/nsf/formats/rtf.rb', line 49

def to_rtf
  #cn = RTF::CommandNode.new('', '')
  #cn.paragraph do |p|
  #  elements = @text.split(/[\*_]/)
  #end
  @text.gsub("\n", " ")
end