Class: ArxivSync::XMLParser
- Inherits:
-
Ox::Sax
- Object
- Ox::Sax
- ArxivSync::XMLParser
- Defined in:
- lib/arxivsync/parser.rb
Instance Attribute Summary collapse
-
#papers ⇒ Object
Returns the value of attribute papers.
Instance Method Summary collapse
- #clean(str) ⇒ Object
- #decode(string) ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize ⇒ XMLParser
constructor
A new instance of XMLParser.
- #start_element(name, attributes = []) ⇒ Object
- #text(str) ⇒ Object
Constructor Details
#initialize ⇒ XMLParser
Returns a new instance of XMLParser.
30 31 32 |
# File 'lib/arxivsync/parser.rb', line 30 def initialize @entities = HTMLEntities.new end |
Instance Attribute Details
#papers ⇒ Object
Returns the value of attribute papers.
28 29 30 |
# File 'lib/arxivsync/parser.rb', line 28 def papers @papers end |
Instance Method Details
#clean(str) ⇒ Object
47 48 49 |
# File 'lib/arxivsync/parser.rb', line 47 def clean(str) str.gsub(/\s+/, ' ').strip end |
#decode(string) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/arxivsync/parser.rb', line 51 def decode(string) str = @entities.decode(string) LaTeX::Decode::Base.normalize(str) LaTeX::Decode::Accents.decode!(str) LaTeX::Decode::Diacritics.decode!(str) LaTeX::Decode::Symbols.decode!(str) str end |
#end_element(name) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/arxivsync/parser.rb', line 118 def end_element(name) case name when :version @model.versions.push(@version) when :metadata # End of a paper entry @papers.push(@model) end @el = nil end |
#start_element(name, attributes = []) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/arxivsync/parser.rb', line 34 def start_element(name, attributes=[]) @el = name case name when :ListRecords @papers = [] when :metadata @model = Paper.new @model.versions = [] when :version @version = Version.new end end |
#text(str) ⇒ Object
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/arxivsync/parser.rb', line 60 def text(str) case @el # Necessary elements when :id @model.id = clean(str) when :submitter @model.submitter = decode(clean(str)) when :title @model.title = decode(clean(str)) when :authors # Author strings may contain strange metadata # Non-regex parsing to handle nested parens depth = 0 no_parens = "" str.chars do |ch| case ch when '(' depth += 1 when ')' depth -= 1 else no_parens << ch if depth == 0 end end @model. = clean(no_parens).split(/,| and /) .map { |s| decode(clean(s)) } .reject { |s| s.empty? } when :categories @model.categories = clean(str).split(/\s/) when :abstract @model.abstract = decode(clean(str)) # Optional elements when :comments @model.comments = decode(clean(str)) when :"msc-class" @model.msc_class = clean(str) when :"report-no" @model.report_no = clean(str) when :"journal-ref" @model.journal_ref = clean(str) when :doi @model.doi = clean(str) when :proxy @model.proxy = clean(str) when :license @model.license = clean(str) # Versions when :date @version.date = Time.parse(clean(str)) when :size @version.size = clean(str) end end |