jekyll-artisync 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-artisync.rb +62 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44f8922f2d59106ea016385d2c1fca07eb4b9a45b46ef8bf9010ba4e671fe6b9
4
+ data.tar.gz: f3317e83d29eaf5a093be418b2e20af1908ece9dc266def4b55984c8f392f6e8
5
+ SHA512:
6
+ metadata.gz: 9d65e4847297fbb8c891e2e203edc49ec881e428df7d01e127a7d0a787265906d484f2363f63be5b14a0d031a328093c0bfd534ba8d231b9a1d4eb125580a744
7
+ data.tar.gz: 1de6cccb4926d3f5d67a94936c2691578c19c5f072b879cb385f9a24da7ee0d222bbfa1784a334e8a1c0929532dcca4254a119035313aea93207cc5b0b891cd3
@@ -0,0 +1,62 @@
1
+ require 'net/http'
2
+ require 'nokogiri'
3
+
4
+ require "jekyll"
5
+
6
+ # user agent is necessary otherwise certain sites such as Zhihu throws 400
7
+ USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'
8
+
9
+ SITE_TO_ARTICLE_XPATH = {
10
+ 'zhihu' => '//div[contains(@class, "Post-RichText") and contains(@class, "ztext")]',
11
+ }
12
+
13
+ class ArticleSyncEmbed < Liquid::Tag
14
+
15
+ def initialize(tagName, content, tokens)
16
+ super
17
+ @content = content
18
+ end
19
+
20
+ def _fetch_html(url)
21
+ uri = URI(url)
22
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
23
+ # :use_ssl => true for the uri is https
24
+ http.request(Net::HTTP::Get.new(uri, {'User-Agent' => USER_AGENT}))
25
+ end
26
+
27
+ res.body
28
+ end
29
+
30
+ def _get_xpath(xpath_type)
31
+
32
+ end
33
+
34
+ def _fetch_article(article_xpath)
35
+ article_html = _fetch_html()
36
+
37
+ xpath = @content.xpath
38
+ article_doc = Nokogiri::HTML(article_html)
39
+
40
+ article_node = article_doc.xpath(article_xpath)
41
+ content = []
42
+ article_node.children.each do |node|
43
+ content.append node.to_html
44
+ end
45
+
46
+ end
47
+
48
+ def render(context)
49
+ url, site = @content.strip.split
50
+ page_html = self._fetch_html(url)
51
+ article = Nokogiri::HTML(page_html).xpath(SITE_TO_ARTICLE_XPATH[site])
52
+ content = []
53
+ article.children.each do |node|
54
+ content.append node.to_html
55
+ end
56
+
57
+ content.join("\n")
58
+ end
59
+
60
+ Liquid::Template.register_tag "artisync", self
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-artisync
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Junhan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Take an article from a given site and display on current Jekyll page.
14
+ email:
15
+ - junhanoct@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jekyll-artisync.rb
21
+ homepage: https://github.com/junhan-z/jekyll-artisync
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Jekyll Plugin to sync articles from other sites.
44
+ test_files: []