jekyll-t4j 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +74 -0
- data/lib/jekyll-t4j/engines/dvisvgm.rb +32 -0
- data/lib/jekyll-t4j/engines.rb +25 -0
- data/lib/jekyll-t4j/merger.rb +47 -0
- data/lib/jekyll-t4j/renderer.rb +72 -0
- data/lib/jekyll-t4j/version.rb +7 -0
- data/lib/jekyll-t4j.rb +34 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13b22b3158377122a8dee5d55e4d57e64dd379ca6dc1af6940fc1eb64bbed5f7
|
4
|
+
data.tar.gz: 6a5b3a578efa74a2f0783301f7b8fa1c3211a0678bc9ad6cea73913cd12b516b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c023286c3b5de954fdec9909e4abccfd4bf8f3b81b9208c616f5c17ef515ad500ab19f9de76b88df47e7eec8389e0eae8512b3f30112c1638bc8dfdd598fc45b
|
7
|
+
data.tar.gz: ff9007ddaedc4bd5839a8ae51523ebc0c188a0188f17d7506c51cbb03f1706dc96e6ac9e70de76a77c40050b9b1d86fd6a9a2790bfa4f4ef33cec0593a82be5c
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 crow02531
|
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,74 @@
|
|
1
|
+
# Jekyll::T4J
|
2
|
+
|
3
|
+
jekyll-t4j is a plugin integrating Jekyll with your local TeX distribution. It enables Jekyll to cope with LaTeX code in your site. If you find MathJax or KaTeX can not cover your needs to write complex articles(i.e. LaTeX using chemfig), this plugin may help you a lot.
|
4
|
+
|
5
|
+
Feel free to write any LaTeX! 🎉
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
We start by a simple instance, write a post `2023-02-04-hello-latex.md` and fill it with:
|
10
|
+
|
11
|
+
```
|
12
|
+
---
|
13
|
+
layout: default
|
14
|
+
title: Hello LaTeX
|
15
|
+
date: 2023-02-04 14:53:32 +0800
|
16
|
+
---
|
17
|
+
|
18
|
+
This is an inline math $$r=\sqrt{x^2+y^2}$$.
|
19
|
+
|
20
|
+
$$\chemfig{*6((--[::-60]HO)-=(-O-[:30])-(-OH)=-=)}$$
|
21
|
+
|
22
|
+
And this is vanillyl alcohol.
|
23
|
+
```
|
24
|
+
|
25
|
+
Then, open your `_config.yml` file and write:
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
t4j:
|
29
|
+
packages:
|
30
|
+
- chemfig
|
31
|
+
```
|
32
|
+
|
33
|
+
Finally, build your site, complete!
|
34
|
+
|
35
|
+
### Configuire
|
36
|
+
|
37
|
+
As shown above, you can configuire jekyll-t4j via `_config.yml`. Here is the template:
|
38
|
+
|
39
|
+
```yaml
|
40
|
+
t4j:
|
41
|
+
packages:
|
42
|
+
- pkgA
|
43
|
+
- pkgB[xxx] # 'xxx' is the option passed to 'pkgB'
|
44
|
+
- pkgC
|
45
|
+
```
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
|
49
|
+
First of all, you need to have a TeX distribution. Just download and install [MikTeX](https://miktex.org/) or [Tex Live](https://tug.org/texlive/).
|
50
|
+
|
51
|
+
Then, install jekyll-t4j just as other Jekyll plugins. Add this line to your application's Gemfile:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
gem 'jekyll-t4j'
|
55
|
+
```
|
56
|
+
|
57
|
+
And then execute:
|
58
|
+
|
59
|
+
```sh
|
60
|
+
$ bundle
|
61
|
+
```
|
62
|
+
|
63
|
+
Or install it yourself as:
|
64
|
+
|
65
|
+
```sh
|
66
|
+
$ gem install jekyll-t4j
|
67
|
+
```
|
68
|
+
|
69
|
+
Lastly, add it to your `_config.yml` file:
|
70
|
+
|
71
|
+
```yaml
|
72
|
+
plugins:
|
73
|
+
- jekyll-t4j
|
74
|
+
```
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "tmpdir"
|
4
|
+
require "jekyll/cache"
|
5
|
+
|
6
|
+
module Jekyll::T4J
|
7
|
+
module Engines
|
8
|
+
@@cache_dvisvgm = Jekyll::Cache.new "Jekyll::T4J::Dvisvgm"
|
9
|
+
|
10
|
+
def self.dvisvgm_raw(src)
|
11
|
+
# setup: write 'src' to 'content.tex'
|
12
|
+
check_tex
|
13
|
+
pwd = Dir.mktmpdir
|
14
|
+
File.write "#{pwd}/content.tex", src
|
15
|
+
|
16
|
+
# call 'latex' to compile: tex->dvi
|
17
|
+
system "latex -halt-on-error content", :chdir => pwd, [:out, :err] => File::NULL, exception: true
|
18
|
+
system "latex -halt-on-error content", :chdir => pwd, [:out, :err] => File::NULL, exception: true
|
19
|
+
# call 'dvisvgm' to convert dvi to svg
|
20
|
+
system "dvisvgm -n -e content", :chdir => pwd, [:out, :err] => File::NULL, exception: true
|
21
|
+
|
22
|
+
# fetch result
|
23
|
+
File.read "#{pwd}/content.svg"
|
24
|
+
ensure
|
25
|
+
FileUtils.remove_entry pwd if pwd
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.dvisvgm(src)
|
29
|
+
@@cache_dvisvgm.getset(src) {dvisvgm_raw(src)}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jekyll::T4J
|
4
|
+
module Engines
|
5
|
+
@@_passed = nil
|
6
|
+
|
7
|
+
def self.has_tex?
|
8
|
+
if @@_passed == nil then
|
9
|
+
@@_passed = system("latex -version", [:out, :err]=>File::NULL) ? true : false
|
10
|
+
end
|
11
|
+
|
12
|
+
@@_passed
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.check_tex
|
16
|
+
unless has_tex?
|
17
|
+
STDERR.puts "You are missing a TeX distribution. Please install:"
|
18
|
+
STDERR.puts " MiKTeX or TeX Live"
|
19
|
+
raise Errors::FatalException.new("Missing TeX distribution")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require "jekyll-t4j/engines/dvisvgm"
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jekyll::T4J
|
4
|
+
module Merger
|
5
|
+
@@available = true
|
6
|
+
@@cache = {}
|
7
|
+
@@rnd_range = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@".chars
|
8
|
+
|
9
|
+
def self.ask_for_merge(url, filedata, extname)
|
10
|
+
raise "Merge request during post processing!" unless @@available
|
11
|
+
|
12
|
+
request = @@cache[url]
|
13
|
+
request = @@cache[url] = {} unless request
|
14
|
+
|
15
|
+
entry = request.rassoc(filedata.freeze)
|
16
|
+
return entry[0] if entry and entry[0].split(".")[1] == extname
|
17
|
+
|
18
|
+
filename = @@rnd_range.sample(22).join.prepend("_") << "." << extname
|
19
|
+
request[filename] = filedata
|
20
|
+
filename.freeze
|
21
|
+
end
|
22
|
+
|
23
|
+
# write external files
|
24
|
+
Jekyll::Hooks.register :documents, :post_write do |doc|
|
25
|
+
@@available = false
|
26
|
+
|
27
|
+
url = doc.url
|
28
|
+
request = @@cache[url]
|
29
|
+
|
30
|
+
if request then
|
31
|
+
url = File.dirname url unless url.end_with? "/"
|
32
|
+
|
33
|
+
request.each {|k, v|
|
34
|
+
File.write File.join(doc.site.dest, url, k), v
|
35
|
+
}
|
36
|
+
|
37
|
+
request.clear
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# clean up
|
42
|
+
Jekyll::Hooks.register :site, :post_write do
|
43
|
+
@@cache.clear
|
44
|
+
@@available = true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "strscan"
|
4
|
+
require "cgi"
|
5
|
+
|
6
|
+
module Jekyll::T4J
|
7
|
+
HTML_TEXT_MASK = Regexp.union(
|
8
|
+
%r{\s*<head[\s\S]*?</head>\s*},
|
9
|
+
%r{\s*<script[\s\S]*?</script>\s*},
|
10
|
+
%r{\s*<style[\s\S]*?</style>\s*},
|
11
|
+
%r{\s*<svg[\s\S]*?</svg>\s*},
|
12
|
+
/\s*<!--.*?-->\s*/,
|
13
|
+
/\s*<.*?>\s*/
|
14
|
+
).freeze
|
15
|
+
|
16
|
+
TEXT_TEX_MASK = Regexp.union(
|
17
|
+
/\\\([\s\S]*?\\\)/,
|
18
|
+
/\\\[[\s\S]*?\\\]/
|
19
|
+
).freeze
|
20
|
+
|
21
|
+
def self.mask(str, reg)
|
22
|
+
s = StringScanner.new str
|
23
|
+
parts = []
|
24
|
+
|
25
|
+
while not s.eos? do
|
26
|
+
p0 = s.charpos
|
27
|
+
m = s.scan_until(reg)
|
28
|
+
|
29
|
+
if m then
|
30
|
+
p1 = s.charpos - s.matched.size - 1
|
31
|
+
|
32
|
+
parts << [str[p0..p1], true] unless p1 <= p0
|
33
|
+
parts << [s.matched, false]
|
34
|
+
else
|
35
|
+
parts << [str[p0..-1], true]
|
36
|
+
break
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
parts
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
Jekyll::Hooks.register :documents, :post_render do |doc|
|
45
|
+
result = String.new
|
46
|
+
|
47
|
+
gen = -> (s) {
|
48
|
+
is_inline = s.start_with?("\\(")
|
49
|
+
|
50
|
+
s.prepend "\\documentclass{article}#{Jekyll::T4J.cfg_pkgs}\\begin{document}\\pagenumbering{gobble}"
|
51
|
+
s << "\\end{document}"
|
52
|
+
s = Jekyll::T4J::Engines.dvisvgm(s)
|
53
|
+
|
54
|
+
"<img src=\"#{Jekyll::T4J::Merger.ask_for_merge(doc.url, s, "svg")}\" style=\"#{
|
55
|
+
is_inline ? "display:inline;height:1.1em;" : "display:block;margin:0 auto;height:calc(0.1em * #{s[/height='(\S+?)pt'/, 1]});"
|
56
|
+
}\">"
|
57
|
+
}
|
58
|
+
|
59
|
+
for p0 in Jekyll::T4J.mask(doc.output, Jekyll::T4J::HTML_TEXT_MASK)
|
60
|
+
if p0[1] then
|
61
|
+
for p1 in Jekyll::T4J.mask(p0[0], Jekyll::T4J::TEXT_TEX_MASK)
|
62
|
+
p1[0] = gen.(CGI::unescapeHTML p1[0]) if not p1[1]
|
63
|
+
|
64
|
+
result << p1[0]
|
65
|
+
end
|
66
|
+
else
|
67
|
+
result << p0[0]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
doc.output = result
|
72
|
+
end
|
data/lib/jekyll-t4j.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "jekyll"
|
4
|
+
|
5
|
+
require "jekyll-t4j/version"
|
6
|
+
|
7
|
+
module Jekyll::T4J
|
8
|
+
@@cfg_pkgs
|
9
|
+
|
10
|
+
def self.cfg_pkgs
|
11
|
+
@@cfg_pkgs
|
12
|
+
end
|
13
|
+
|
14
|
+
# initialize plugin
|
15
|
+
Jekyll::Hooks.register :site, :after_init do |site|
|
16
|
+
cfg = site.config["t4j"]
|
17
|
+
@@cfg_pkgs = (cfg and (cfg = cfg["packages"])) ? parse_cfg_pkgs(cfg) : ""
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.parse_cfg_pkgs(cfg)
|
21
|
+
ret = String.new
|
22
|
+
|
23
|
+
for p in cfg
|
24
|
+
raise "Illegal config: #{p}" unless p.match(/\s*([\w-]+)(\[[^\[\]]*\])?\s*/)
|
25
|
+
ret << "\\usepackage#{$2}{#{$1}}"
|
26
|
+
end
|
27
|
+
|
28
|
+
ret
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
require "jekyll-t4j/merger"
|
33
|
+
require "jekyll-t4j/engines"
|
34
|
+
require "jekyll-t4j/renderer"
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-t4j
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- crow02531
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.1'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
description:
|
34
|
+
email: crow02531@outlook.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- lib/jekyll-t4j.rb
|
42
|
+
- lib/jekyll-t4j/engines.rb
|
43
|
+
- lib/jekyll-t4j/engines/dvisvgm.rb
|
44
|
+
- lib/jekyll-t4j/merger.rb
|
45
|
+
- lib/jekyll-t4j/renderer.rb
|
46
|
+
- lib/jekyll-t4j/version.rb
|
47
|
+
homepage: https://github.com/crow02531/jekyll-t4j
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements:
|
66
|
+
- TeX distribution
|
67
|
+
rubygems_version: 3.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: LaTeX support for Jekyll.
|
71
|
+
test_files: []
|