-
Notifications
You must be signed in to change notification settings - Fork 4
/
10_biber.rb
105 lines (90 loc) · 3.65 KB
/
10_biber.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# frozen_string_literal: true
# Copyright 2010-2018, Raphael Reitzig
#
# This file is part of ltx2any.
#
# ltx2any is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ltx2any is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.
require 'English'
Dependency.new('biber', :binary, [:extension, 'Biber'], :essential)
# TODO: document
class Biber < Extension
def initialize
super
@name = 'Biber'
@description = 'Creates bibliographies (recommended)'
@sources = []
end
def do?(time)
return false unless time == 1
params = ParameterManager.instance
usesbib = File.exist?("#{params[:jobname]}.bcf")
needrerun = false
if usesbib
# Collect sources (needed for log parsing)
@sources = []
File.foreach("#{params[:jobname]}.bcf") do |line|
if %r{<bcf:datasource[^>]*type="file"[^>]*>(.*?)</bcf:datasource>} =~ line
@sources.push($LAST_MATCH_INFO[1])
end
end
@sources.uniq!
# Aside from the first run (no bbl),
# there are two things that prompt us to rerun:
# * changes to the bcf file (which includes all kinds of things,
# including the actual citations)
# * changes to the bib sources (which are listed in the bcf file)
needrerun = !File.exist?("#{params[:jobname]}.bbl") | # Is this the first run?
HashManager.instance.files_changed?("#{params[:jobname]}.bcf",
*@sources)
# NOTE: non-strict OR so that hashes are computed for next run
end
usesbib && needrerun
end
def exec(_time, _progress)
params = ParameterManager.instance
# Command to process bibtex bibliography if necessary.
# Uses the following variables:
# * jobname -- name of the main LaTeX file (without file ending)
biber = "biber '#{params[:jobname]}'"
f = IO.popen(biber)
log = f.readlines
# Dig trough output and find errors
msgs = []
errors = false
linectr = 1
log.each do |line|
loglines = { from: linectr, to: linectr }
case line
when /^INFO - (.*)$/
msgs.push(TexLogParser::Message.new(message: $LAST_MATCH_INFO[1], log_lines: loglines, level: :info))
when /^WARN - (.*)$/
msgs.push(TexLogParser::Message.new(message: $LAST_MATCH_INFO[1], log_lines: loglines, level: :warning))
when %r{^ERROR - BibTeX subsystem: .*?(#{@sources.map do |s|
Regexp.escape(s)
end.join('|')}).*?, line (\d+), (.*)$}
srclines = { from: Integer($LAST_MATCH_INFO[2]), to: Integer($LAST_MATCH_INFO[2]) }
msgs.push(TexLogParser::Message.new(message: $LAST_MATCH_INFO[3].strip, source_file: $LAST_MATCH_INFO[1], source_lines: srclines,
log_lines: loglines, level: :error))
errors = true
when /^ERROR - (.*)$/
msgs.push(TexLogParser::Message.new(message: $LAST_MATCH_INFO[1], log_lines: loglines, level: error))
errors = true
end
linectr += 1
end
{ success: !errors, messages: msgs, log: log.join.strip! }
end
end
Extension.add Biber