Class: Hiki2md

Inherits:
Object
  • Object
show all
Defined in:
lib/hiki2md.rb,
lib/hiki2md/version.rb

Constant Summary collapse

IMAGE_EXTENSIONS =
%w[.jpg .jpeg .gif .png .bmp .svg].freeze
UNSAFE_URI_SCHEMES =
%w[javascript data vbscript].freeze
MAX_MARKDOWN_LIST_DEPTH =
100
MAX_BLOCKQUOTE_RECURSION =
100
PLUGIN_TOKEN_RE =
/\0(\d+)\0/
/\[\[.+?\]\]/
URI_RE =
/(?:https?|ftp|file|mailto):[A-Za-z0-9;\/?:@&=+$,\-_.!~*'()#%]+/
WIKI_NAME_RE =
/\b(?:[A-Z]+[a-z\d]+){2,}\b/
MODIFIER_RE =
/'''.+?'''|''.+?''|==.+?==|``.+?``/
INLINE_RE =
Regexp.union(
  /\0\d+\0/,
  BRACKET_LINK_RE,
  URI_RE,
  MODIFIER_RE,
  /\^?#{WIKI_NAME_RE}/
)
VERSION =
"0.3.2"

Instance Method Summary collapse

Constructor Details

#initialize(interwiki_map: {}, use_wiki_name: true, preserve_plugins: false) ⇒ Hiki2md

Returns a new instance of Hiki2md.



24
25
26
27
28
29
# File 'lib/hiki2md.rb', line 24

def initialize(interwiki_map: {}, use_wiki_name: true, preserve_plugins: false)
  @interwiki_map = interwiki_map
  @use_wiki_name = use_wiki_name
  @preserve_plugins = preserve_plugins
  @conversion_mutex = Mutex.new
end

Instance Method Details

#convert(source) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/hiki2md.rb', line 31

def convert(source)
  @conversion_mutex.synchronize do
    normalized = source.to_s.gsub(/\r\n?/, "\n")
    nul_replacement = normalized.encoding == Encoding::UTF_8 ? "\uFFFD" : '?'
    normalized = normalized.gsub("\0", nul_replacement)
    @source_template = normalized[0, 0]
    escaped = extract_plugins(normalized)
    convert_lines(escaped.split(/\n/))
  end
end

#make_matrix(contents) ⇒ Object

Legacy public API. Conversion itself uses the structure-preserving table renderer below, but callers of these helpers retain the previous output.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hiki2md.rb', line 44

def make_matrix(contents)
  matrix = []
  contents.each do |line|
    row = line.split('||')
    row.shift
    matrix << row
  end

  matrix.each_with_index do |line, row_index|
    line.each_with_index do |cell, column_index|
      next unless cell =~ /\^+/

      matrix[row_index][column_index] = Regexp.last_match.post_match
      Regexp.last_match.size.times do |offset|
        matrix[row_index + offset + 1] ||= []
        matrix[row_index + offset + 1].insert(column_index, ' ')
      end
    end
  end

  max_columns = 0
  matrix.each_with_index do |line, row_index|
    column_count = line.size
    target_column = 0
    line.each do |cell|
      if cell =~ />+/
        matrix[row_index][target_column] = Regexp.last_match.post_match
        Regexp.last_match.size.times do
          target_column += 1
          matrix[row_index][target_column] = ''
        end
        column_count += Regexp.last_match.size
      else
        matrix[row_index][target_column] = cell
        target_column += 1
      end
    end
    max_columns = column_count if column_count > max_columns
  end

  [matrix, max_columns]
end

#make_table(table_contents) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hiki2md.rb', line 87

def make_table(table_contents)
  contents, max_columns = make_matrix(table_contents)
  alignment = '|'
  max_columns.times { alignment << ':----|' }
  alignment << "\n"
  table = "\n"

  contents.each_with_index do |line, index|
    row = '|'
    line.each { |cell| row << "#{cell}|" }
    table << row << "\n"
    table << alignment if index.zero?
  end

  table
end