wikimovia 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ module Wikimovia
2
+ module Formatters
3
+ class DateFormatter
4
+ include Wikimovia::Utilities
5
+
6
+ FILM_DATE_PATTERN = /\{\{film date\|(.*)\}\}/m
7
+ YEAR_PATTERN = /^\d{4}$/
8
+
9
+ # Formats a date string based on the following conditions:
10
+ # * If it is of the format {film date|year|month|day}, it will return a
11
+ # {Date} matching those values.
12
+ # * If it is a year, it will return the year.
13
+ # * Otherwise it returns the string as it was.
14
+ def format(string)
15
+ case string
16
+ when YEAR_PATTERN then string.to_i
17
+ when FILM_DATE_PATTERN
18
+ string = extract_pattern(FILM_DATE_PATTERN, string)
19
+ numbers = string.split('|').map(&:to_i)
20
+ Date.new(numbers[0], numbers[1], numbers[2])
21
+ else string
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module Wikimovia
2
+ module Formatters
3
+ class LinkFormatter
4
+ include Wikimovia::Utilities
5
+
6
+ PATTERN = /\[\[(.*)\]\]/
7
+ LINK_PATTERN = /^.*\|(.*)$/
8
+
9
+ class <<self
10
+ def match?(string)
11
+ string.match(PATTERN) ? true : false
12
+ end
13
+ end
14
+
15
+ # If the string provided is wrapped in '[[' and ']]', returns the
16
+ # contents of those wrappers. If it is of the format
17
+ # [linked article|link name] it will return the link name.
18
+ def format(string)
19
+ contents = extract_pattern(PATTERN, string)
20
+
21
+ if contents.match(LINK_PATTERN)
22
+ extract_pattern(LINK_PATTERN, contents)
23
+ else
24
+ contents
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module Wikimovia
2
+ module Formatters
3
+ class PlainlistFormatter
4
+ include Wikimovia::Utilities
5
+
6
+ PATTERN = Wikimovia::Utilities.template_regex('plainlist')
7
+
8
+ def self.match?(string)
9
+ string.match(PATTERN) ? true : false
10
+ end
11
+
12
+ # Returns plainlist items as an array.
13
+ def format(string)
14
+ string = extract_pattern(PATTERN, string)
15
+ string.strip!
16
+ link_formatter = LinkFormatter.new
17
+ string.split('*').reject(&:empty?).map(&:strip)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Wikimovia
2
+ module Formatters
3
+ class TimeFormatter
4
+ include Wikimovia::Utilities
5
+
6
+ MINUTES_PATTERN = /^(\d+) minutes$/
7
+
8
+ # If the string matches the pattern of 'NUMBER minutes', returns NUMBER.
9
+ # otherwise returns the string.
10
+ def format(string)
11
+ case string
12
+ when MINUTES_PATTERN
13
+ extract_pattern(MINUTES_PATTERN, string).to_i * 60
14
+ else string
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ module Wikimovia
2
+ class TemplateExtractor
3
+ include Wikimovia::Utilities
4
+
5
+ # Given some wikitext syntax, and a :template_name, extracts the first
6
+ # template with that name it can find and converts it to a {Hash}.
7
+ def extract_first(string, options = {})
8
+ pattern = Wikimovia::Utilities.template_regex options[:template_name],
9
+ use_vertical_bar: false
10
+
11
+ wikitext = WikiCloth::Parser.new(data: string).to_wikitext
12
+ template = extract_pattern(pattern, wikitext)
13
+
14
+ if template
15
+ info_lines = extract_info_lines(template)
16
+ split_lines = split_infolines(info_lines)
17
+ cleaned_lines = clean_infolines(split_lines)
18
+ filtered_lines = filter_infolines(cleaned_lines)
19
+ hash = Hash[*filtered_lines.flatten]
20
+ hash.keys.each do |key|
21
+ hash[(key.to_sym rescue key) || key] = hash.delete(key)
22
+ end
23
+ hash
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # Get all info lines and reject any empty ones.
30
+ def extract_info_lines(string)
31
+ string.split(/\n^\|/).reject(&:empty?)
32
+ end
33
+
34
+ # Take each infoline and split it into an array of [key, value]
35
+ def split_infolines(info_lines)
36
+ info_lines.map { |info_line| info_line.split("=") }
37
+ end
38
+
39
+ # Take each info line array and strip each of its values
40
+ def clean_infolines(info_lines)
41
+ info_lines.map do |info_line|
42
+ info_line.map(&:strip)
43
+ end
44
+ end
45
+
46
+ # Remove any infolines that don't have both a key and a value.
47
+ def filter_infolines(info_lines)
48
+ info_lines.
49
+ reject { |line| line.any?(&:empty?) }.
50
+ reject { |line| line.size != 2 }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,16 @@
1
+ module Wikimovia
2
+ module Utilities
3
+ def self.template_regex(template_name, options = {})
4
+ options[:use_vertical_bar] = true if options[:use_vertical_bar].nil?
5
+ if options[:use_vertical_bar]
6
+ /\{\{#{template_name}.*\|(.*)\n\}\}/m
7
+ else
8
+ /\{\{#{template_name}(.*)\n\}\}/m
9
+ end
10
+ end
11
+
12
+ def extract_pattern(pattern, string)
13
+ string.scan(pattern).flatten.compact.first
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikimovia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -49,6 +49,12 @@ executables: []
49
49
  extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
+ - lib/wikimovia/formatters/date_formatter.rb
53
+ - lib/wikimovia/formatters/link_formatter.rb
54
+ - lib/wikimovia/formatters/plainlist_formatter.rb
55
+ - lib/wikimovia/formatters/time_formatter.rb
56
+ - lib/wikimovia/template_extractor.rb
57
+ - lib/wikimovia/utilities.rb
52
58
  - lib/wikimovia.rb
53
59
  homepage: https://github.com/alex-tan/wikimovia
54
60
  licenses: []