lib/asciidoctor/iso/ref.rb in asciidoctor-iso-0.7.9 vs lib/asciidoctor/iso/ref.rb in asciidoctor-iso-0.8.0
- old
+ new
@@ -33,11 +33,11 @@
date.on matched[:from]
end
end
def use_my_anchor(ref, id)
- ref.parent["id"] = id
+ ref.parent.children.last["id"] = id
ref
end
def isorefmatches(xml, m)
ref = fetch_ref xml, m[:code], m[:year]
@@ -80,44 +80,71 @@
end
def fetch_year_check(hit, code, year, opts)
ret =nil
if year.nil? || year.to_i == hit.hit["year"]
- ret = hit.to_xml opts
- @bibliodb[code] = ret
- else
- warn "WARNING: cited year #{year} does not match year "\
+ ret = hit.to_xml opts
+ @bibliodb[code] = ret if @bibliodb
+ @local_bibliodb[code] = ret if @local_bibliodb
+ else
+ warn "WARNING: cited year #{year} does not match year "\
"#{hit.hit['year']} found on the ISO website for #{code}"
- end
+ end
ret
end
+ def first_with_title(result)
+ result.first.each do |x|
+ next unless x.hit["title"]
+ return x
+ end
+ return nil
+ end
+
def first_year_match_hit(result, code, year)
- return result&.first&.first if year.nil?
+ return first_with_title(result) if year.nil?
return nil unless result.first && result.first.is_a?(Array)
coderegex = %r{^(ISO|IEC)[^0-9]*\s[0-9-]+}
result.first.each do |x|
- return x if x.hit["title"]&.match(coderegex)&.to_s == code &&
+ next unless x.hit["title"]
+ return x if x.hit["title"].match(coderegex).to_s == code &&
year.to_i == x.hit["year"]
end
- return result&.first&.first
+ return first_with_title(result)
end
- def fetch_ref1(code, year, opts)
- return @bibliodb[code] if @bibliodb[code]
+ def fetch_ref_err(code)
+ warn "WARNING: no match found on the ISO website for #{code}."
+ if /\d-\d/.match? code
+ warn "The provided document part may not exist, or the document may no longer be published in parts."
+ else
+ warn "If you wanted to cite all document parts for the reference, use #{code}:All Parts"
+ end
+ end
+
+ def fetch_ref2(code, year, opts)
result = Isobib::IsoBibliography.search(code)
ret = nil
hit = first_year_match_hit(result, code, year)
coderegex = %r{^(ISO|IEC)[^0-9]*\s[0-9-]+}
if hit && hit.hit["title"]&.match(coderegex)&.to_s == code
ret = fetch_year_check(hit, code, year, opts)
else
- warn "WARNING: no match found on the ISO website for #{code}"
+ fetch_ref_err(code)
end
ret
end
+ def fetch_ref1(code, year, opts)
+ return nil if @bibliodb.nil? # signals we will not be using isobib
+ @bibliodb[code] = fetch_ref2(code, year, opts) unless @bibliodb[code]
+ @local_bibliodb[code] = @bibliodb[code] if !@local_bibliodb.nil? &&
+ !@local_bibliodb[code]
+ return @local_bibliodb[code] unless @local_bibliodb.nil?
+ @bibliodb[code]
+ end
+
def fetch_ref(xml, code, year, **opts)
warn "fetching #{code}..."
hit = fetch_ref1(code, year, opts)
return nil if hit.nil?
xml.parent.add_child(hit)
@@ -199,27 +226,31 @@
reference1(node, item.text, xml)
end
end.join("\n")
end
- def bibliocache_name()
- "#{Dir.home}/.asciidoc-iso-biblio-cache.json"
+ def bibliocache_name(global)
+ global ? "#{Dir.home}/.relaton-bib.json" :
+ "#{@filename}.relaton.json"
end
- def open_cache_biblio(node)
- filename = bibliocache_name()
+ # if returns nil, then biblio caching is disabled, and so is use of isobib
+ def open_cache_biblio(node, global)
+ return nil # disabling for now
+ filename = bibliocache_name(global)
system("rm -f #{filename}") if node.attr("flush-caches") == "true"
biblio = {}
if Pathname.new(filename).file?
File.open(filename, "r") do |f|
biblio = JSON.parse(f.read)
end
end
biblio
end
- def save_cache_biblio(biblio)
- filename = bibliocache_name()
+ def save_cache_biblio(biblio, global)
+ return if biblio.nil?
+ filename = bibliocache_name(global)
File.open(filename, "w") do |b|
b << biblio.to_json
end
end
end