module Md2pukiwiki

Constants

VERSION

Public Class Methods

convert(text) click to toggle source
# File lib/md2pukiwiki.rb, line 4
def self.convert(text)
  decorated_type = %w{bold italic strikethrough}
  prefixed_type = %w{header list numbered_list}
  special_type = %w{image link}

  text.lines.map do |line|
    new_line = line.chomp

    [decorated_type, prefixed_type, special_type].flatten.each do |type|
      new_line = self.send("convert_#{type}", new_line)
    end

    new_line
  end.join("\n")
end

Private Class Methods

convert_bold(line) click to toggle source

bold” => “''bold''”

# File lib/md2pukiwiki.rb, line 50
def self.convert_bold(line)
  line.gsub(/(?:\*{2}|_{2})(?<bold>.+?)(?:\*{2}|_{2})/, "''\\k<bold>''")
end
convert_header(line) click to toggle source

“# header” => “*header”

# File lib/md2pukiwiki.rb, line 23
def self.convert_header(line)
  if /\A(?<sharps>#+) (?<header>.+)\z/ =~ line
    "#{'*' * sharps.length}#{header}"
  else
    line
  end
end
convert_image(line) click to toggle source

“![text](image)” => “#ref(image,text)”

# File lib/md2pukiwiki.rb, line 65
def self.convert_image(line)
  line.gsub(/!\[(?<text>.+?)\]\((?<image>.+?)\)/, '#ref(\k<image>,\k<text>)')
end
convert_italic(line) click to toggle source

italic” => “'''italic'''”

# File lib/md2pukiwiki.rb, line 55
def self.convert_italic(line)
  line.gsub(/(?:\*{1}|_{1})(?<italic>.+?)(?:\*{1}|_{1})/, "'''\\k<italic>'''")
end
convert_list(line) click to toggle source

“* list” => “- list”

# File lib/md2pukiwiki.rb, line 32
def self.convert_list(line)
  if /\A(?<spaces>\s*)\* (?<list>.+)\z/ =~ line
    "#{'-' * (spaces.length / 4 + 1)} #{list}"
  else
    line
  end
end
convert_numbered_list(line) click to toggle source

“1. list” => “+ list”

# File lib/md2pukiwiki.rb, line 41
def self.convert_numbered_list(line)
  if /\A(?<spaces>\s*)\d+\. (?<list>.+)\z/ =~ line
    "#{'+' * (spaces.length / 4 + 1)} #{list}"
  else
    line
  end
end
convert_strikethrough(line) click to toggle source

“~~strikethrough~~” => “%%strikethrough%%”

# File lib/md2pukiwiki.rb, line 60
def self.convert_strikethrough(line)
  line.gsub(/~{2}(?<strikethrough>.+?)~{2}/, '%%\k<strikethrough>%%')
end