Class: Zimki

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

Instance Method Summary collapse

Instance Method Details

#apply_textile_conversion_rules(text) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/zimki.rb', line 117

def apply_textile_conversion_rules(text)
  w = %w(remove_content_type_textile
        remove_wiki_format
        remove_creation_date
        remove_big_header
        remove_created
        h_one_title
        h_two_title
        h_three_title
        convert_link_to_textile
        convert_italique_to_textile
        convert_bold_to_textile
        convert_highlight_to_textile
        convert_image
        convert_third_bullet
        convert_second_bullet
        convert_first_bullet
        )

  tmp = text

  w.each do |function|
    converted_text = send function, tmp
    if tmp.chop == "'''"
      # tbd special routine for verbatim environment
      puts "Verbatim"
    end
    tmp = converted_text if converted_text != "no match"
  end

  tmp
end

#compare_files(src, dst) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zimki.rb', line 89

def compare_files(src, dst)
  one = File.open(src)
  two = File.open(dst)

  one_text = ""
  while line_text_one = one.gets
    one_text << line_text_one
  end

  two_text = ""
  while line_text_two = two.gets
    two_text << line_text_two
  end

  one_text == two_text
end

#convert_bold_to_textile(text) ⇒ Object



30
31
32
# File 'lib/zimki.rb', line 30

def convert_bold_to_textile(text)
  text.gsub("**", "*")
end

#convert_first_bullet(text) ⇒ Object



42
43
44
# File 'lib/zimki.rb', line 42

def convert_first_bullet(text)
  show_regexp(text, /\t\* ([\\A-Za-z0-9"'?<>=:!?_\/\n]*)/, "first_bullet")
end

#convert_highlight_to_textile(text) ⇒ Object



34
35
36
# File 'lib/zimki.rb', line 34

def convert_highlight_to_textile(text)
  text.gsub("__", "@")
end

#convert_image(text) ⇒ Object



74
75
76
77
78
# File 'lib/zimki.rb', line 74

def convert_image(text)
  image = show_regexp(text, /\{\{([A-Za-z0-9~\/.:!?_\s-]*)\}\}/, "image")
  image = image.gsub("{{", "!")
  image = image.gsub("}}", "!")
end

#convert_italique_to_textile(text) ⇒ Object



25
26
27
28
# File 'lib/zimki.rb', line 25

def convert_italique_to_textile(text)
  text = text.gsub("//", "_")
  text.gsub("http:_", "http://")
end


54
55
56
# File 'lib/zimki.rb', line 54

def convert_link_to_textile(text)
  show_regexp(text, /\[\[http[A-Za-z0-9.-:]*\|[A-Za-z.]*\]\]|http[A-Za-z0-9.-:]*/, "link")
end

#convert_second_bullet(text) ⇒ Object



46
47
48
# File 'lib/zimki.rb', line 46

def convert_second_bullet(text)
  show_regexp(text, /\t\t\* ([\\A-Za-z0-9"'?<>=:!?_\/\n]*)/, "second_bullet")
end

#convert_third_bullet(text) ⇒ Object



50
51
52
# File 'lib/zimki.rb', line 50

def convert_third_bullet(text)
  show_regexp(text, /\t\t\t\* ([\\A-Za-z0-9"'?<>=:!?_\/\n]*)/, "third_bullet")
end

#convert_verbatim_to_textile(text) ⇒ Object



58
59
60
# File 'lib/zimki.rb', line 58

def convert_verbatim_to_textile(text)
  show_regexp(text, /'''\n([A-Za-z0-9"'?<>!?_\t\n]*)(\n''')/, "verbatim")
end

#copy_test_file(src, dst) ⇒ Object



80
81
82
83
# File 'lib/zimki.rb', line 80

def copy_test_file(src, dst)
  FileUtils.cp src, dst
  load_test_file(dst)
end

#h_one_title(text) ⇒ Object



62
63
64
# File 'lib/zimki.rb', line 62

def h_one_title(text)
  show_regexp(text, /====([A-Za-z0-9.:!?_\s-]*)====/, "h_one_header")
end

#h_three_title(text) ⇒ Object



70
71
72
# File 'lib/zimki.rb', line 70

def h_three_title(text)
  show_regexp(text, /==([A-Za-z0-9.:!?_\s-]*)==\n/, "h_three_header")
end

#h_two_title(text) ⇒ Object



66
67
68
# File 'lib/zimki.rb', line 66

def h_two_title(text)
  show_regexp(text, /===([A-Za-z0-9.:!?_\s-]*)===\n/, "h_two_header")
end

#load_test_file(file) ⇒ Object



85
86
87
# File 'lib/zimki.rb', line 85

def load_test_file(file)
  File.exists?(file)
end

#remove_big_header(text) ⇒ Object



17
18
19
# File 'lib/zimki.rb', line 17

def remove_big_header(text)
  show_regexp(text, /======([A-Za-z0-9.:!?_\s-]*)======/, "big_header")
end

#remove_content_type_textile(text) ⇒ Object



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

def remove_content_type_textile(text)
  show_regexp(text, /Content-Type:([A-Za-z0-9\/:<>!?_\s-]*)/, "content_type")
end

#remove_created(text) ⇒ Object



21
22
23
# File 'lib/zimki.rb', line 21

def remove_created(text)
  show_regexp(text, /Created([A-Za-z0-9.:!?_\s-]*)\n/, "created")
end

#remove_creation_date(text) ⇒ Object



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

def remove_creation_date(text)
  show_regexp(text, /Creation-Date:([A-Za-z0-9.:!?_\s-]*)/, "creation_date")
end

#remove_wiki_format(text) ⇒ Object



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

def remove_wiki_format(text)
  show_regexp(text, /Wiki-Format:[A-Za-z0-9.\s]*/, "wiki_format")
end

#replace_leading_newlines(text) ⇒ Object



38
39
40
# File 'lib/zimki.rb', line 38

def replace_leading_newlines(text)
  text.gsub("\n\n\n", "")
end

#show_regexp(string, pattern, type) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/zimki.rb', line 150

def show_regexp(string, pattern, type)
  match = string.match(pattern)
  if match
    if type == "link"
     matching_string = match[0]
     if !matching_string.include?("[[")
      "#{match.pre_match}\"#{matching_string}\":#{matching_string}#{match.post_match}"
     else
      split = matching_string.split("|")
      "#{match.pre_match}\"#{split[1].gsub("]]", "\"")}#{split[0].gsub("[[", ":")}#{match.post_match}"
    end
    elsif type == "verbatim"
      "#{match[0].gsub("'''\n", "{% highlight plain %}\n").gsub("\n'''", "\n{% endhighlight %}")}"
    elsif type == "content_type"
      ""
    elsif type == "wiki_format"
      ""
    elsif type == "creation_date"
      ""
    elsif type == "big_header"
      ""
    elsif type == "created"
      ""
    elsif type == "first_bullet"
     "#{match[0].gsub("\t*", "**")}#{match.post_match}"
    elsif type == "second_bullet"
     "#{match[0].gsub("\t\t*", "***")}#{match.post_match}"
    elsif type == "third_bullet"
      "#{match[0].gsub("\t\t\t*", "****")}#{match.post_match}"
    elsif type == "h_one_header"
     "#{match[0].gsub("==== ", "h1. ").gsub("====", "")}\n\n\n"
    elsif type == "h_two_header"
     "#{match[0].gsub("=== ", "h2. ").gsub("===", "")}"
    elsif type == "h_three_header"
     "#{match[0].gsub("== ", "h3. ").gsub("==", "")}"
    elsif type == "image"
      "#{match[0].gsub("{{", "!http://").gsub("}}", "!")}"
    else
      raise "None type has been specified"
    end
  else
    "no match"
  end
end

#textile_conversion(file) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/zimki.rb', line 106

def textile_conversion(file)
  source = File.open(file)
  text = ""

  while line = source.gets
    text << apply_textile_conversion_rules(line)
  end

  replace_leading_newlines(text)
end