Module: Jekyll::Gdocfilter

Defined in:
lib/jekyll/gdocfilter.rb,
lib/jekyll/gdocfilter/version.rb

Overview

Parses a google doc link to neatly embedable html, using the google_doc method.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#convert_headings(html) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jekyll/gdocfilter.rb', line 66

def convert_headings(html)
  # Convert  <span class=title>s to <h1 class=title> elements
  # Downsize headings by 1
  (5).downto(1) do |n|
    heading = html.at_css "h#{n}"
    heading.name = "h#{n + 1}" if heading
  end

  title = html.at_css "p.title"
  title.name = "h1" if title

  @html = html
end

#get_file(url) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/jekyll/gdocfilter.rb', line 99

def get_file(url)
  begin
    f = URI.open url
  rescue OpenURI::HTTPError
    return
  end
  return unless f.status[1] == "OK"

  f
end

#get_html(link) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jekyll/gdocfilter.rb', line 84

def get_html(link)
  ids = /[-\w]{25,}/.match(link)
  return unless ids

  @url = URI(link)
  return unless @url.host == "docs.google.com"

  link = "https://docs.google.com/feeds/download/documents/export/Export?id=#{ids[0]}&exportFormat=html"

  f = get_file(link)
  return unless f

  @html = Nokogiri::HTML.parse f
end


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll/gdocfilter.rb', line 16

def get_query_from_link(link)
  return unless link

  href = URI(link)
  return link unless href.query

  query = CGI.parse(href.query)
  return href unless query["q"][0]

  query["q"][0]
end

#google_doc(link) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/jekyll/gdocfilter.rb', line 110

def google_doc(link)
  return unless get_html(link)

  replace_links @html
  inline_styles @html
  convert_headings @html
  # TODO: Properly nest lists
  @html.css("body").inner_html
end

#inline_styles(html) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/gdocfilter.rb', line 53

def inline_styles(html)
  css = reduce_styles html.css("style").inner_html
  css.each_selector do |selector, declarations, _specificity|
    next unless selector =~ /^[\d\w\s\#.\-]*$/ # Check if is real selector

    elements = html.css(selector)
    elements.each do |e|
      e["style"] = [e["style"], declarations].compact.join(" ")
    end
  end
  @html = html
end

#parser_cssObject



80
81
82
# File 'lib/jekyll/gdocfilter.rb', line 80

def parser_css
  @parser_css ||= CssParser::Parser.new
end

#reduce_styles(css) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll/gdocfilter.rb', line 39

def reduce_styles(css)
  # Extract basic styling from the <style> tag at top of document
  # ie. bold, colours
  # But not any of the layout/spacing/font-type styles
  allowed_rules = %w[font-weight font-style text-decoration]
  parser_css.add_block! css
  parser_css.each_rule_set do |rule_set|
    rule_set.each_declaration do |d|
      rule_set.remove_declaration! d unless allowed_rules.include? d
    end
  end
  @parser_css
end


28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll/gdocfilter.rb', line 28

def replace_links(html)
  # Removes the google.com proxy from links in the doc
  html.css("a").each do |link|
    next unless link.attributes["href"]

    href = link.attributes["href"].value
    link.attributes["href"].value = get_query_from_link(href)
  end
  @html = html
end