Skip to content

Commit

Permalink
Refactoring: Use new class Bookname.
Browse files Browse the repository at this point in the history
  • Loading branch information
janfri committed Apr 29, 2016
1 parent fdc626f commit faab8dc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/scripref.rb
@@ -1,5 +1,6 @@
# - encoding: utf-8 -
require 'delegate'
require 'scripref/bookname'
require 'scripref/parser'
require 'scripref/passage'
require 'scripref/processor'
Expand Down
16 changes: 14 additions & 2 deletions lib/scripref/english.rb
@@ -1,13 +1,13 @@
# - encoding: utf-8 -
require 'scripref/bookname'
require 'scripref/const_reader'

module Scripref

# Mixin for parsing references in English.
module English

# Array of book names.
BOOK_NAMES = <<-END.strip.split(/,\s*/)
book_names = <<-END.strip.split(/,\s*/)
Genesis, Exodus, Leviticus, Numbers, Deuteronomy, Joshua, Judges,
Ruth, 1 Samuel, 2 Samuel, 1 Kings, 2 Kings, 1 Chronicles,
2 Chronicles, Ezra, Nehemiah, Esther, Job, Psalms, Proverbs,
Expand All @@ -21,6 +21,18 @@ module English
Jude, Revelation
END

book_abbrevs = <<-END.strip.split(/,\s*/)
Gen, Ex, Lev, Num, Deut, Josh, Judg, Rth, 1 Sam, 2 Sam, 1 Kgs, 2 Kgs,
1 Chron, 2 Chron, Ezr, Neh, Esth, Job, Ps, Prov, Eccles, Song, Isa,
Jer, Lam, Ezek, Dan, Hos, Joel, Am, Obad, Jon, Mic, Nah, Hab, Zeph, Hag,
Zech, Mal, Matt, Mrk, Luk, John, Acts, Rom, 1 Cor, 2 Cor, Gal, Eph, Phil,
Col, 1 Thess, 2 Thess, 1 Tim, 2 Tim, Tit, Philem, Heb, Jas, 1 Pet, 2 Pet,
1 Joh, 2 Joh, 3 Joh, Jud, Rev
END

# Array of book names.
BOOK_NAMES = book_names.zip(book_abbrevs).map {|name, abbrev| Bookname.new(name, abbrev)}

# Separator between chapter and verse.
CV_SEPARATOR = ':'

Expand Down
9 changes: 5 additions & 4 deletions lib/scripref/formatter.rb
Expand Up @@ -4,13 +4,14 @@ module Scripref

class Formatter

attr_accessor :cv_separator, :hyphen_separator, :pass_separator

attr_accessor :bookformat, :cv_separator, :hyphen_separator, :pass_separator

# @param mods one or more modules to include
def initialize *mods
# @param bookformat (:short use abbreviations, :long use full names of books)
def initialize *mods, bookformat: :name
@mods = mods
mods.each {|m| extend m}
@bookformat = bookformat
end

# Formats a reference (array of passages and maybe separators)
Expand All @@ -28,7 +29,7 @@ def format *reference
# Formats a book
# @param num number of book (starting at 1)
def format_book num
Array(book_names[num - 1]).first
book_names[num - 1].send @bookformat
end

# Formats a chapter
Expand Down
15 changes: 13 additions & 2 deletions lib/scripref/german.rb
@@ -1,13 +1,13 @@
# - encoding: utf-8 -
require 'scripref/bookname'
require 'scripref/const_reader'

module Scripref

# Mixin for parsing references in German.
module German

# Array of book names.
BOOK_NAMES = <<-END.strip.split(/,\s*/).map {|e| e.split('|')}
book_names = <<-END.strip.split(/,\s*/).map {|e| e.split('|')}
1. Mose, 2. Mose, 3. Mose, 4. Mose, 5. Mose, Josua, Richter, Ruth, 1. Samuel, 2. Samuel,
1. Könige, 2. Könige, 1. Chronika, 2. Chronika, Esra, Nehemia, Esther, Hiob, Psalm|Psalmen,
Sprüche, Prediger, Hohelied, Jesaja, Jeremia, Klagelieder, Hesekiel, Daniel, Hosea, Joel,
Expand All @@ -18,6 +18,17 @@ module German
2. Johannes, 3. Johannes, Judas, Offenbarung
END

book_abbrevs = <<-END.strip.split(/,\s*/).map {|e| e.split('|')}
1.Mo, 2.Mo, 3.Mo, 4.Mo, 5.Mo, Jos, Ri, Ruth, 1.Sam, 2.Sam, 1.Kön, 2.Kön, 1.Chr, 2.Chr,
Esr, Neh, Est, Hi, Ps, Spr, Pred, Hohel, Jes, Jer, Klag, Hes, Dan, Hos, Joel, Amos, Obad,
Jona, Mich, Nah, Hab, Zef, Hag, Sach, Mal, Mat, Mar, Luk, Joh, Apg, Röm, 1.Ko, 2.Ko,
Gal, Eph, Phil, Kol, 1.Thes, 2.Thes, 1.Tim, 2.Tim, Tit, Philem, Heb, Jak, 1.Pet, 2.Pet,
1.Joh, 2.Joh, 3.Joh, Jud, Off
END

# Array of book names.
BOOK_NAMES = book_names.zip(book_abbrevs).map {|name, abbrev| Bookname.new(name, abbrev)}

# Separator between chapter and verse.
CV_SEPARATOR = ','

Expand Down
16 changes: 9 additions & 7 deletions lib/scripref/parser.rb
Expand Up @@ -256,9 +256,11 @@ def push_passage
# Regular expression to match a book.
def book_re
return @book_re if @book_re
books_res_as_strings = book_names.flatten.map do |bn|
(bn.gsub(/([^\dA-Z])/, '\1?').gsub('.', '\.')) << '\b\s*'
end
books_res_as_strings = book_names.map do |bn|
bn.names.map do |n|
(n.gsub(/([^\dA-Z])/, '\1?').gsub('.', '\.')) << '\b\s*'
end
end.flatten
@book_re = Regexp.compile(books_res_as_strings.map {|s| '(^' << s << ')' }.join('|'), nil)
end

Expand All @@ -271,7 +273,7 @@ def abbrev2book str
if name = special_abbrev_mapping[s]
return name
end
@books_str ||= ('#' << book_names.flatten.join('#') << '#')
@books_str ||= ('#' << book_names.map(&:names).flatten.join('#') << '#')
pattern = s.chars.map {|c| Regexp.escape(c) << '[^#]*'}.join
re = /(?<=#)#{pattern}(?=#)/
names = @books_str.scan(re)
Expand All @@ -285,9 +287,9 @@ def abbrev2book str
def book2num str
unless @book2num
@book2num = {}
book_names.each_with_index do |bns, i|
Array(bns).each do |bn|
@book2num[bn] = i+1
book_names.each_with_index do |bn, i|
bn.names.each do |n|
@book2num[n] = i+1
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_english.rb
Expand Up @@ -12,6 +12,10 @@ def setup
@parser = Parser.new(English)
end

def test_size_of_book_array
assert_equal 66, English::BOOK_NAMES.size
end

def test_book_re
book_re = @parser.book_re
assert_match book_re, 'Genesis'
Expand Down
17 changes: 17 additions & 0 deletions test/test_formatter.rb
Expand Up @@ -256,6 +256,23 @@ def test_complex_example
assert_formated_text_for_ast text, ast
end

######################################################################
# Formatting of books
######################################################################

def test_formatting_with_book_abbrevs
@german_formatter.bookformat = :abbrev
text = 'Mat 3,4; Mar; Joh 3,16'
t1, t2, t3 = text.split(semi)
assert_formated_text_for_ast text, [pass(t1, 40, 3, 4, 40, 3, 4), semi, pass(t2, 41, nil, nil, 41, nil, nil), semi, pass(t3, 43, 3, 16, 43, 3, 16)]
end

def test_exception_for_unhandled_bookformat
assert_raise NoMethodError do
@german_formatter.bookformat = :unknown
@german_formatter.format [pass(1,2,3,4,5,6,7)]
end
end
private

def assert_formated_text_for_ast text, ast
Expand Down
4 changes: 4 additions & 0 deletions test/test_german.rb
Expand Up @@ -12,6 +12,10 @@ def setup
@parser = Parser.new(German)
end

def test_size_of_book_array
assert_equal 66, German::BOOK_NAMES.size
end

def test_book_re
book_re = @parser.book_re
assert_match book_re, '1. Mose'
Expand Down

0 comments on commit faab8dc

Please sign in to comment.