Class: Asuka::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/asuka/formatter.rb

Instance Method Summary collapse

Instance Method Details

#bold(line) ⇒ Object



9
10
11
# File 'lib/asuka/formatter.rb', line 9

def bold(line)
  line.gsub(/\*\*([^*]+)\*\*/, '<strong>\1</strong>')
end

#default_stepsObject



5
6
7
# File 'lib/asuka/formatter.rb', line 5

def default_steps
  [:bold, :italic, :named_link]
end

#italic(line) ⇒ Object



13
14
15
# File 'lib/asuka/formatter.rb', line 13

def italic(line)
  line.gsub(/\*([^*]+)\*/,     '<em>\1</em>')
end

helper



18
19
20
21
22
23
24
# File 'lib/asuka/formatter.rb', line 18

def make_link(text, href, tags={})
  tags          = tags.merge(:href => href.strip)
  tags_to_attrs = tags.sort_by { |name, value| name.to_s }.
                       map     { |name, value| %Q{ #{name}="#{value}"} }

  "<a#{tags_to_attrs}>#{text.strip}</a>"
end


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/asuka/formatter.rb', line 26

def named_link(line)
  # up to ] (spaces allowed)
  up_to                 = /[^\]]+?/
  brackets_without_http = /\[(#{up_to})\]/

  # up to ] (spaces NOT allowed)
  up_to                 = /[^\]\s]+/
  brackets_with_http    = /\[\s*(https?:\/\/#{up_to})\s*\]/

  line.gsub(/#{brackets_without_http}#{brackets_with_http}/) { |match| make_link($1, $2) }.
       gsub(/#{brackets_with_http}#{brackets_without_http}/) { |match| make_link($2, $1) }
end