jekyll-quicklatex 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80a9071c584ad61bd0e434ebea1c30d648d6d892
4
+ data.tar.gz: bd42c14b27ca99b2ee9c10e249a182800fc00220
5
+ SHA512:
6
+ metadata.gz: c2d4b5ad80ee5c5642c8f1e31ad1686417fda0c8df513a3450e6485c58ef4583d8823aeaea62c0ef01a18a46fcd95886e55a926787958d6a31d11bf86f8956a2
7
+ data.tar.gz: fc160826045cd01e252eb62838154a799981fac0a302ca0837994ba09d477039350bacf0890e1e02a30b6b6fb6cb34c4c8b33b3a95fee7d4a3160ae49e862891
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ _site/
14
+ .sass-cache/
15
+ .jekyll-metadata
16
+
17
+ *.gem
18
+
19
+ /assets/
20
+ latex.cache
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.0
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jekyll-quicklatex.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 David Zhang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Jekyll::Quicklatex
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jekyll/quicklatex`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jekyll-quicklatex'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jekyll-quicklatex
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll-quicklatex. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Jekyll::Quicklatex project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jekyll-quicklatex/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jekyll/quicklatex/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-quicklatex"
8
+ spec.version = Jekyll::Quicklatex::VERSION
9
+ spec.authors = ["DreamAndDead"]
10
+ spec.email = ["aquairain@gmail.com"]
11
+
12
+ spec.summary = %q{a jekyll converter plugin for latex tikz.}
13
+ spec.description = %q{jekyll-quicklatex, a jekyll converter plugin for latex tikz.}
14
+ spec.homepage = "https://github.com/DreamAndDead/jekyll-quicklatex"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.15"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "webmock", "~>2.0"
26
+
27
+ spec.add_development_dependency "jekyll", "~> 3.0"
28
+ end
@@ -0,0 +1,148 @@
1
+ require 'net/http'
2
+ require 'fileutils'
3
+ require 'digest'
4
+ require 'jekyll/quicklatex/version'
5
+
6
+ module Jekyll
7
+ module Quicklatex
8
+ class Block < Liquid::Block
9
+ def initialize tag_name, markup, tokens
10
+ super
11
+ init_param
12
+ end
13
+
14
+ def render(context)
15
+ @output_dir = context.registers[:site].config['destination']
16
+ snippet = filter_snippet(super)
17
+ url = remote_compile snippet
18
+ "<img src='/#{@saved_dir}#{url}' onerror='this.onerror=null; this.src=\"http://quicklatex.com#{url}\"' />#{super}"
19
+ end
20
+
21
+ private
22
+
23
+ class Cache
24
+ def initialize
25
+ @cache = {}
26
+ @cache_file = 'latex.cache'
27
+ if File.exist? @cache_file
28
+ File.open(@cache_file, 'r') do |f|
29
+ while line = f.gets
30
+ hash, url = line.split
31
+ @cache[hash] = url
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def fetch(content)
38
+ id = hash_id(content)
39
+ @cache[id]
40
+ end
41
+
42
+ def cache(content, url)
43
+ id = hash_id(content)
44
+ @cache[id] = url
45
+ File.open(@cache_file, 'a') do |f|
46
+ f.syswrite("#{id} #{url}\n")
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def hash_id(content)
53
+ Digest::MD5.hexdigest(content)
54
+ end
55
+ end
56
+
57
+ def init_param
58
+ @site_uri = URI('http://quicklatex.com/latex3.f')
59
+ @post_param = {
60
+ :fsize => '30px',
61
+ :fcolor => '000000',
62
+ :mode => 0,
63
+ :out => 1,
64
+ :errors => 1,
65
+ :remhost => 'quicklatex.com',
66
+ :rnd => '9.483426365449255'
67
+ }
68
+ @pic_regex = /http.*png/
69
+ @saved_dir = 'assets/latex'
70
+ @cache = Cache.new
71
+ end
72
+
73
+ def filter_snippet(snippet)
74
+ # text is html that rendered by highlight
75
+ # strip all html tags
76
+ no_html_tag = snippet.gsub(/<\/?[^>]*>/, "")
77
+ .gsub(/&gt;/, '>')
78
+ .gsub(/&lt;/, '<')
79
+
80
+ # strip all comments in latex code snippet
81
+ lines = no_html_tag.lines
82
+ lines.reject do |l|
83
+ # blank line or comments(start with %)
84
+ l =~ /^\s*$/ or l =~ /^\s*%/
85
+ end.join
86
+ end
87
+
88
+ def seperate_snippet(snippet)
89
+ lines = snippet.lines
90
+ preamble, formula = lines.partition { |line| line =~ /usepackage/ }
91
+
92
+ def join_back(lines)
93
+ lines.join('').gsub(/%/, '%25').gsub(/&/, '%26')
94
+ end
95
+
96
+ return {
97
+ :preamble => join_back(preamble),
98
+ :formula => join_back(formula),
99
+ }
100
+ end
101
+
102
+ def remote_compile(snippet)
103
+ if url = @cache.fetch(snippet)
104
+ return url
105
+ end
106
+
107
+ param = @post_param.merge(seperate_snippet(snippet))
108
+
109
+ req = Net::HTTP::Post.new(@site_uri)
110
+ body_raw = param.inject('') do |result, nxt|
111
+ "#{result}&#{nxt[0].to_s}=#{nxt[1].to_s}"
112
+ end
113
+ req.body = body_raw.sub('&', '')
114
+
115
+ res = Net::HTTP.start(@site_uri.hostname, @site_uri.port) do |http|
116
+ http.request(req)
117
+ end
118
+
119
+ case res
120
+ when Net::HTTPSuccess, Net::HTTPRedirection
121
+ pic_uri = URI(res.body[@pic_regex])
122
+
123
+ save_path = "#{@saved_dir}#{pic_uri.path}"
124
+ dir = File.dirname(save_path)
125
+ unless File.directory? dir
126
+ FileUtils.mkdir_p dir
127
+ end
128
+
129
+ @cache.cache(snippet, save_path)
130
+
131
+ Net::HTTP.start(pic_uri.host) do |http|
132
+ # http get
133
+ resp = http.get(pic_uri.path)
134
+ File.open(save_path, "wb") do |file|
135
+ file.write(resp.body)
136
+ end
137
+ end
138
+
139
+ pic_uri.path
140
+ else
141
+ res.value
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ Liquid::Template.register_tag('latex', Jekyll::Quicklatex::Block)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Quicklatex
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-quicklatex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - DreamAndDead
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jekyll
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: jekyll-quicklatex, a jekyll converter plugin for latex tikz.
84
+ email:
85
+ - aquairain@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - jekyll-quicklatex.gemspec
99
+ - lib/jekyll/quicklatex.rb
100
+ - lib/jekyll/quicklatex/version.rb
101
+ homepage: https://github.com/DreamAndDead/jekyll-quicklatex
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.6.12
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: a jekyll converter plugin for latex tikz.
125
+ test_files: []