Class: Octopress::Printable::ImgConverter

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

Constant Summary collapse

TITLE_REGEX =
/title:['|"](.+?)['|"]/

Instance Attribute Summary

Attributes inherited from Converter

#match

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Converter

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

Constructor Details

#initialize(source_dir) ⇒ ImgConverter

Returns a new instance of ImgConverter.



7
8
9
10
11
# File 'lib/octopress-printable/img.rb', line 7

def initialize(source_dir)
  super()

  @source_dir = source_dir
end

Class Method Details

.get_img_label(markup) ⇒ Object

from octopress-image-tag plugin



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/octopress-printable/img.rb', line 27

def self.get_img_label(markup)
  title = markup.scan(TITLE_REGEX).flatten.compact.last
  markup.gsub!(TITLE_REGEX, '')

  if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d\S+))?(?:\s+(?<height>\d\S+))?(?<alt>\s+.+)?/i
    attributes = ['class', 'src', 'width', 'height', 'alt']
    image = attributes.reduce({}) { |img, attr| img[attr] ||= $~[attr].strip if $~[attr]; img }
    text = image['alt']

    # Allow parsing "title" "alt"
    if text =~ /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/
      image['title']  = title
      image['alt']    = alt
    else
      # Set alt text and title from text
      image['alt'].gsub!(/"/, '') if image['alt']
    end
  end

  image['title'] ||= title
  image['alt'] ||= title

  image
end

Instance Method Details

#convert(line) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/octopress-printable/img.rb', line 13

def convert(line)
  str = line

  if /{% img (?<markup>.*) %}/ =~ str
    @match = true

    img = ImgConverter.get_img_label(markup)
    str="\\begin{figure}[h]\\centering\\includegraphics[width=\\textwidth]{#{@source_dir}/#{img['src']}}\\caption{#{img['title']}}\\label{#{img['alt']}}\\end{figure}"
  end
 
  str
end

#get_includes(post, source_dir) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/octopress-printable/img.rb', line 58

def get_includes(post, source_dir)
  includes = []
  File.open(post, 'r') do |f|
    while l = f.gets
      if /{% img (?<markup>.*) %}/ =~ l
        img = ImgConverter.get_img_label(markup)
        includes << File.join(source_dir, img['src']) 
      end
    end
  end
  includes
end

#headerObject



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

def header
  lines = []
  lines << '\\usepackage{graphicx}'
  lines << '\\usepackage[all]{hypcap}'
end