thredded-markdown_katex 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: c909cb1c04a74a744529f645d77d1faee3236ff1
4
+ data.tar.gz: cce8776eb2cd2729c7aa4747b74797d5334331e3
5
+ SHA512:
6
+ metadata.gz: be17249c94f1137a52711a9a3e7e1e84bd72e45e01e4a9566ce50bbe92f91be0e120b4e8dfaf8bad2db580453ddcd429c6fbc91598b6975a4ac042058d3097df
7
+ data.tar.gz: f441d02a24a8cdaafeb03c0856fedc34eba6db6c280589bb9be356273eb2c24ecde142959c22ee7221590154de38b4abcf82b16692bead8dbece5c4ed959e364
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Gleb Mazovetskiy
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Thredded Plugin: TeX math in Markdown via KaTex [![Build Status](https://travis-ci.org/thredded/thredded-markdown_katex.svg?branch=master)](https://travis-ci.org/thredded/thredded-markdown_katex) [![Test Coverage](https://codeclimate.com/github/thredded/thredded-markdown_katex/badges/coverage.svg)](https://codeclimate.com/github/thredded/thredded-markdown_katex/coverage) [![Code Climate](https://codeclimate.com/github/thredded/thredded-markdown_katex/badges/gpa.svg)](https://codeclimate.com/github/thredded/thredded-markdown_katex)
2
+
3
+ A Thredded markdown plugin that renders TeX math server-side via [KaTeX].
4
+
5
+ This is what it looks like:
6
+
7
+ ![screenshot](https://cloud.githubusercontent.com/assets/216339/22184709/a7799d12-e0cf-11e6-89dc-a0bf2206f98e.png)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'thredded-markdown_katex', '~> 0.1.0'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundle
21
+ rails g thredded:markdown_katex:install
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Wrap math in `$$`.
27
+
28
+ See the [Kramdown Math documentation](https://kramdown.gettalong.org/syntax.html#math-blocks)
29
+ for more information.
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies.
34
+ Then, run `rake spec` to run the tests.
35
+ You can also run `bin/console` for an interactive prompt that will allow you to
36
+ experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`.
39
+ To release a new version, update the version number in `version.rb`,
40
+ and then run `bundle exec rake release`, which will create a git tag for
41
+ the version, push git commits and tags, and push the `.gem` file to
42
+ [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at
47
+ https://github.com/glebm/thredded-markdown_katex.
48
+
49
+ This project is intended to be a safe, welcoming space for collaboration,
50
+ and contributors are expected to adhere
51
+ to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of
57
+ the [MIT License](http://opensource.org/licenses/MIT).
58
+
59
+
60
+ [KaTeX]: https://github.com/Khan/KaTeX
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ module Thredded
3
+ module MarkdownKatex
4
+ # Installs Thredded Katex plugin
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc 'Installs Thredded Katex plugin.'
7
+ public_task :install
8
+
9
+ def install # rubocop:disable Metrics/MethodLength
10
+ scss_path = 'app/assets/stylesheets/application.scss'
11
+ if File.exist? scss_path
12
+ append_to_file scss_path, "\n" + '@import "katex";' + "\n"
13
+ end
14
+ sass_path = 'app/assets/stylesheets/application.sass'
15
+ if File.exist? sass_path
16
+ append_to_file sass_path, "\n" + '@import "katex"' + "\n"
17
+ end
18
+ css_path = 'app/assets/stylesheets/application.css'
19
+ if File.exist? css_path # rubocop:disable Style/GuardClause
20
+ append_to_file sass_path, "\n" + '//= require katex' + "\n"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ module Thredded
3
+ module MarkdownKatex
4
+ module Kramdown
5
+ # The KaTeX converter engine for Kramdown.
6
+ module KatexConverter
7
+ def self.call(_converter, el, _opts)
8
+ type = el.options[:category]
9
+ Katex.render(
10
+ el.value, display_mode: type == :block, throw_on_error: false
11
+ )
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'kramdown/converter'
19
+ Kramdown::Converter.module_eval do
20
+ add_math_engine :katex do |converter, el, opts|
21
+ add_math_engine(:katex, ::Thredded::MarkdownKatex::Kramdown::KatexConverter)
22
+ math_engine(:katex).call(converter, el, opts)
23
+ end
24
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+ module Thredded
3
+ module MarkdownKatex
4
+ module MathMLWhitelist
5
+ # All the presentation MathML elements and attributes, as per the
6
+ # MathML Version 3.0 3rd Edition schema:
7
+ # https://www.w3.org/Math/draft-spec/appendixa.html
8
+
9
+ # Attributes common to all MathML elements
10
+ COMMON_ATT = %w(id xref class style href).freeze
11
+ # Attributes common to presentational elements
12
+ COMMON_PRES_ATT = %w(mathcolor mathbackground).freeze
13
+ # Attributes common to token elements
14
+ TOKEN_ATT = %w(mathvariant mathsize dir).freeze
15
+ # Attributes common to elements that can be indented
16
+ INDENT_ATT = %w(
17
+ indentalign indentshift indenttarget indentalignfirst indentshiftfirst
18
+ indentalignlast indentshiftlast
19
+ ).freeze
20
+ # Attributes common to definition elements
21
+ DEF_ENC_ATT = %w(definitionURL encoding).freeze
22
+
23
+ # All of these elements have MATHML_COMMON_PRES_ATT.
24
+ PRESENTATION_WHITELIST = {
25
+ 'maction' => %w(actiontype selection),
26
+ 'menclose' => %w(notation),
27
+ 'merror' => %w(),
28
+ 'mfenced' => %w(open close separators),
29
+ 'mfrac' =>
30
+ %w(bevelled denomalign linethickness numalign),
31
+ 'mglyph' => %w(src width height valign alt),
32
+ 'mpadded' =>
33
+ %w(height depth width lspace voffset),
34
+ 'mphantom' => %w(),
35
+ 'mroot' => %w(),
36
+ 'mrow' => %w(dir),
37
+ 'msqrt' => %w(),
38
+ 'mstyle' => INDENT_ATT + %w(
39
+ scriptlevel displaystyle scriptsizemultiplier scriptminsize
40
+ infixlinebreakstyle decimalpoint accent accentunder align
41
+ alignmentscope bevelled charalign charspacing close columnalign
42
+ columnlines columnspacing columnspan columnwidth crossout
43
+ denomalign depth dir edge equalcolumns equalrows fence form frame
44
+ framespacing groupalign height largeop leftoverhang length
45
+ linebreak linebreakmultchar linebreakstyle lineleading
46
+ linethickness location longdivstyle lquote lspace mathsize
47
+ mathvariant maxsize minlabelspacing minsize movablelimits
48
+ mslinethickness notation numalign open position rightoverhang
49
+ rowalign rowlines rowspacing rowspan rquote rspace selection
50
+ separator separators shift side stackalign stretchy
51
+ subscriptshift superscriptshift symmetric valign width
52
+ ),
53
+ 'mi' => TOKEN_ATT,
54
+ 'mn' => TOKEN_ATT,
55
+ 'mo' => TOKEN_ATT + INDENT_ATT +
56
+ %w(form fence separator lspace rspace stretchy symmetric minsize
57
+ maxsize largeop movablelimits accent linebreak lineleading
58
+ linebreakstyle linebreakmultchar),
59
+ 'ms' => TOKEN_ATT + %w(lquote rquote),
60
+ 'mspace' => TOKEN_ATT + INDENT_ATT +
61
+ %w(width height depth linebreak),
62
+ 'mtext' => TOKEN_ATT,
63
+ 'maligngroup' => %w(groupalign),
64
+ 'malignmark' => %w(edge),
65
+ 'mlabeledtr' => %w(rowalign columnalign groupalign),
66
+ 'mtable' => %w(
67
+ align rowalign columnalign groupalign alignmentscope columnwidth
68
+ width rowspacing columnspacing rowlines columnlines frame
69
+ framespacing equalrows equalcolumns displaystyle side
70
+ minlabelspacing
71
+ ),
72
+ 'mtd' => %w(rowspan columnspan rowalign columnalign groupalign),
73
+ 'mtr' => %w(rowalign columnalign groupalign),
74
+ 'mmultiscripts' => %w(superscriptshift subscriptshift),
75
+ 'msub' => %w(subscriptshift),
76
+ 'msup' => %w(superscriptshift),
77
+ 'msubsup' => %w(superscriptshift subscriptshift),
78
+ 'munder' => %w(accentunder align),
79
+ 'mover' => %w(accent align),
80
+ 'munderover' => %w(accent accentunder align),
81
+ 'mlongdiv' => %w(position shift longdivstyle),
82
+ 'mscarries' => %w(position location crossout scriptsizemultiplier),
83
+ 'mscarry' => %w(location crossout),
84
+ 'msgroup' => %w(position shift),
85
+ 'msline' =>
86
+ %w(position length leftoverhang rightoverhang mslinethickness),
87
+ 'none' => %w(),
88
+ 'mprescripts' => %w(),
89
+ 'msrow' => %w(position),
90
+ 'mstack' => %w(align stackalign charalign charspacing)
91
+ }.freeze
92
+
93
+ OTHER_WHITELIST = {
94
+ 'math' => %w(xmlns display maxwidth overflow altimg altimg-width
95
+ altimg-height altimg-valign alttext cdgroup),
96
+ 'semantics' => DEF_ENC_ATT,
97
+ 'annotation' => %w(cd name src) + DEF_ENC_ATT,
98
+ 'annotation-xml' => %w(cd name src) + DEF_ENC_ATT
99
+ }.freeze
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ module Thredded
3
+ module MarkdownKatex
4
+ # Registers the thredded-markdown_katex initializer with Rails
5
+ class Railtie < Rails::Railtie
6
+ initializer 'thredded.markdown_katex', after: 'thredded' do
7
+ Thredded::MarkdownKatex.setup!
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Thredded
3
+ module MarkdownKatex
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ require 'thredded/markdown_katex/version'
3
+ require 'katex'
4
+ require 'thredded/markdown_katex/railtie' if defined?(Rails)
5
+ require 'thredded/markdown_katex/mathml_whitelist'
6
+
7
+ module Thredded
8
+ # Adds TeX math support to Thredded markdown via KaTeX.
9
+ module MarkdownKatex
10
+ @options = {
11
+ math_engine: 'katex',
12
+ math_engine_opts: {}
13
+ }
14
+
15
+ KRAMDOWN_FILTER_CLASS_NAME = 'Thredded::HtmlPipeline::KramdownFilter'
16
+
17
+ class << self
18
+ attr_reader :options
19
+
20
+ def setup!
21
+ if pipeline_contains?(KRAMDOWN_FILTER_CLASS_NAME)
22
+ require 'thredded/markdown_katex/kramdown/katex_converter'
23
+ Thredded::HtmlPipeline::KramdownFilter.options.update(options)
24
+ else
25
+ raise "#{self.class.name} requires #{KRAMDOWN_FILTER_CLASS_NAME} " \
26
+ 'in Thredded::ContentFormatter.pipeline_filters'
27
+ end
28
+
29
+ configure_whitelist!
30
+ end
31
+
32
+ private
33
+
34
+ def pipeline_contains?(name)
35
+ Thredded::ContentFormatter.pipeline_filters.map(&:name).include?(name)
36
+ end
37
+
38
+ def configure_whitelist!
39
+ whitelist_katex_html!
40
+ whitelist_mathml!
41
+ end
42
+
43
+ def whitelist_katex_html!
44
+ whitelist_element! 'span', %w(class style aria-hidden)
45
+ Thredded::ContentFormatter.whitelist[:css] ||= {}
46
+ Thredded::ContentFormatter.whitelist[:css][:properties] ||= []
47
+ Thredded::ContentFormatter.whitelist[:css][:properties] += %w(
48
+ color width height vertical-align margin-left margin-right font-size
49
+ top
50
+ )
51
+ end
52
+
53
+ def whitelist_mathml!
54
+ MathMLWhitelist::PRESENTATION_WHITELIST.each do |tag, attrs|
55
+ whitelist_element! tag, MathMLWhitelist::COMMON_ATT +
56
+ MathMLWhitelist::COMMON_PRES_ATT + attrs
57
+ end
58
+
59
+ MathMLWhitelist::OTHER_WHITELIST.each do |tag, attrs|
60
+ whitelist_element! tag, MathMLWhitelist::COMMON_ATT + attrs
61
+ end
62
+ end
63
+
64
+ def whitelist_element!(tag, attributes)
65
+ whitelist_el = Thredded::ContentFormatter.whitelist[:elements]
66
+ whitelist_el << tag unless whitelist_el.include?(tag)
67
+ whitelist_attr = Thredded::ContentFormatter.whitelist[:attributes]
68
+ whitelist_attr[tag] ||= []
69
+ whitelist_attr[tag] += attributes
70
+ whitelist_attr[tag].uniq!
71
+ end
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thredded-markdown_katex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Mazovetskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: katex
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: thredded
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Adds TeX math support to Thredded markup via KaTeX.
112
+ email:
113
+ - glex.spb@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - lib/generators/thredded/markdown_katex/install_generator.rb
121
+ - lib/thredded/markdown_katex.rb
122
+ - lib/thredded/markdown_katex/kramdown/katex_converter.rb
123
+ - lib/thredded/markdown_katex/mathml_whitelist.rb
124
+ - lib/thredded/markdown_katex/railtie.rb
125
+ - lib/thredded/markdown_katex/version.rb
126
+ homepage: https://github.com/thredded/thredded-markdown_katex
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.3'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.6.9
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Adds TeX math support to Thredded markup via KaTeX.
150
+ test_files: []