Class: Octopress::Printable::BibConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/octopress-printable/bib.rb

Instance Attribute Summary

Attributes inherited from Converter

#match

Instance Method Summary collapse

Methods inherited from Converter

#envs, get_includes, #last_xelatex, #pandoc_args, timestamp, #xelatex_args

Constructor Details

#initialize(src_bib, tgt_bib) ⇒ BibConverter

Returns a new instance of BibConverter.



5
6
7
8
9
10
# File 'lib/octopress-printable/bib.rb', line 5

def initialize(src_bib, tgt_bib)
  super()

  @src_bib = src_bib
  @tgt_bib = tgt_bib
end

Instance Method Details

#before_xelatex(step, texfile) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/octopress-printable/bib.rb', line 58

def before_xelatex(step, texfile)
  cmds = []
  if step == 2
	  cmds << "bibtex #{texfile.sub(/\.tex$/, "")} >/dev/null"
  end
  cmds
end

#cleanupObject



49
50
51
# File 'lib/octopress-printable/bib.rb', line 49

def cleanup()
  FileUtils.rm_f(@tgt_bib)
end

#convert(line) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/octopress-printable/bib.rb', line 12

def convert(line)
  str = line
  if /##*\s*References/ =~ str
    return ""
  end

  if /{% bibliography .*? %}/ =~ str
    @match = true

    str = "\\bibliographystyle{unsrt}\\bibliography{#{@tgt_bib.sub(/\..*$/, "")}}"

    gen_bib("#{@src_bib}", "#{@tgt_bib}")
  end

  while /{% cite\s+(?<citation>.*?)\s+%}/ =~ str
    @match = true
    citation = citation.sub(/\s+/, ',')
    str = str.sub(/{% cite\s+(.*?)\s+%}/, "\\cite{#{citation}}")
  end

  str
end

#gen_bib(obib, bib) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/octopress-printable/bib.rb', line 35

def gen_bib(obib, bib)
  File.open(obib, 'r') do |f|
    File.open(bib, 'w') do |o|
      while l = f.gets
        if /<a\s+href="?(?<url>.*?)"?\s*>(?<text>.*?)<\/a>/ =~ l
          l = l.sub(/<a\s+href=(.*?)>(.*?)<\/a>/, "\\href{#{url}}{#{text}}")
        end

        o.puts l
      end
    end
  end
end

#headerObject



53
54
55
56
# File 'lib/octopress-printable/bib.rb', line 53

def header
  lines = []
  lines << '\\usepackage[sort&compress, numbers]{natbib}'
end