Class: Octopress::Tags::QuoteTag::Tag

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/octopress-quote-tag.rb

Constant Summary collapse

FullCiteWithTitle =
/(\S.*)\s+(https?:\/\/)(\S+)\s+(.+)/i
FullCite =
/(\S.*)\s+(https?:\/\/)(\S+)/i
AuthorTitle =
/([^,]+),([^,]+)/
Author =
/(.+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Tag

Returns a new instance of Tag.



13
14
15
16
17
18
19
20
21
# File 'lib/octopress-quote-tag.rb', line 13

def initialize(tag_name, markup, tokens)
  super

  if tag_name.strip == 'blockquote'
    @options = parse_legacy_markup(markup)
  else
    @options = parse_markup(%w{author title url}, markup)
  end
end

Instance Method Details

#authorObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/octopress-quote-tag.rb', line 93

def author
  if @options['author']
    if !@options['title']
      text = link(@options['author'])
    else
      text = @options['author']
    end
    "<span class='quote-author'>#{text}</span>"
  end
end

#extract(input, regexp, indices_to_try = [1], default = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/octopress-quote-tag.rb', line 33

def extract(input, regexp, indices_to_try = [1], default = nil)
  thing = input.match(regexp)
  if thing.nil?
    default
  else
    indices_to_try.each do |index|
      return thing[index] if thing[index]
    end
  end
end

#figcaptionObject



87
88
89
90
91
# File 'lib/octopress-quote-tag.rb', line 87

def figcaption
  if @options['author'] || @options['url'] || @options['title']
    "<figcaption class='quote-source'>#{author || ''}#{title || ''}</figcaption>"
  end
end


104
105
106
107
108
109
110
# File 'lib/octopress-quote-tag.rb', line 104

def link(text)
  if @options['url']
    "<a class='quote-link' href='#{@options['url']}'>#{text}</a>"
  else
    text
  end
end

#parse_content(content, context) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/octopress-quote-tag.rb', line 71

def parse_content(content, context)
  path  = context.environments.first['page']['path']
  ext   = File.extname(path[1..-1])[1..-1]
  site  = context.registers[:site]
  mdext = site.config['markdown_ext']
  txext = site.config['textile_ext']

  if mdext.include? ext
    site.getConverterImpl(Jekyll::Converters::Markdown).convert(content)
  elsif txext.include? ext
    site.getConverterImpl(Jekyll::Converters::Textile).convert(content)
  else
    "<p>" + content.strip.gsub(/\n\n/, "<p>\n\n</p>") + "</p>"
  end
end

#parse_legacy_markup(markup) ⇒ Object

Use legacy regex matching to parse markup



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/octopress-quote-tag.rb', line 45

def parse_legacy_markup(markup)
  options = {}
  if markup =~ FullCiteWithTitle
    options['author'] = $1
    options['url'] = $2 + $3
    options['title'] = $4.strip
  elsif markup =~ FullCite
    options['author'] = $1
    options['url'] = $2 + $3
  elsif markup =~ AuthorTitle
    options['author'] = $1
    options['title'] = $2.strip
  elsif markup =~ Author
    options['author'] = $1
  end
  options
end

#parse_markup(keys, markup) ⇒ Object

Parse string into hash object



24
25
26
27
28
29
30
31
# File 'lib/octopress-quote-tag.rb', line 24

def parse_markup(keys, markup)
  options = {}
  keys.each do |k|
    value = extract(markup, /\s*#{k}:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6])
    options[k] = value
  end
  options
end

#render(context) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/octopress-quote-tag.rb', line 63

def render(context)
  quote = "<blockquote>#{parse_content(super, context).strip}</blockquote>"
  if cap = figcaption
    quote = "<figure class='quote'>#{quote}#{cap}</figure>"
  end
  quote
end

#titleObject



112
113
114
115
116
# File 'lib/octopress-quote-tag.rb', line 112

def title
  if @options['title']
    "<cite class='quote-title'>#{link(@options['title'])}</cite>"
  end
end

#trim_url(full_url) ⇒ Object



118
119
120
121
122
123
# File 'lib/octopress-quote-tag.rb', line 118

def trim_url(full_url)
  parts = []
  short_url = full_url.match(/https?:\/\/(.+)/)[1][0..30]
  short_url << '' unless short_url == full_url
  short_url
end