Class: Jekyll::GDocsToJekyll

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/gdocs2jekyll.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, doc_id, token) ⇒ GDocsToJekyll

Returns a new instance of GDocsToJekyll.



12
13
14
15
# File 'lib/gdocs2jekyll.rb', line 12

def initialize(tag_name, doc_id, token)
  super
  @docId = doc_id.rstrip.lstrip
end

Instance Method Details

#render(context) ⇒ Object



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
# File 'lib/gdocs2jekyll.rb', line 17

def render(context)
  gDocsURL = "https://www.googleapis.com/drive/v3/files/#{@docId}/export"
  params = {
    :mimeType => "text/html",
    :key => ENV["GOOGLE_DRIVE_API_KEY"]
  }
  res = HTTP.get(gDocsURL, :params => params)


  if res.code != 200
    "#{res.to_s()}"
    return
  end

  resBody = res.body.to_s()
  parsedHTML = Nokogiri::HTML5(resBody)

  parsedHTML.search("*").each do |node|
    if node.name == "span" and node["style"]
      if node["style"].include? "font-style:italic"
        semantic_swap(parsedHTML, node, "i")
      elsif node["style"].include? "font-weight:700"
        semantic_swap(parsedHTML, node, "b")
      end
    end

    if not ["img", "a"].include? node.name
      node.keys.each do |attr|
        node.delete(attr)
      end
    end

    if node.name == "a" and not node["href"]
      parsedHTML.delete(node)
    end

    if(node.name == "span")
      unwrap(node)
    end
  end

    mddoc = PandocRuby.convert("#{parsedHTML.to_html}",
                               from: :html,
                               to: :markdown_mmd)
                      .force_encoding("UTF-8")

    "#{mddoc}"
end

#semantic_swap(doc, node, semantic_tag) ⇒ Object



66
67
68
69
70
# File 'lib/gdocs2jekyll.rb', line 66

def semantic_swap(doc, node, semantic_tag)
  semantic_node = doc.create_element(semantic_tag)
  semantic_node.inner_html = node.inner_html
  node.replace(semantic_node)
end

#unwrap(node) ⇒ Object



72
73
74
75
76
# File 'lib/gdocs2jekyll.rb', line 72

def unwrap(node)
  if node.parent
    node.replace(node.inner_html)
  end
end