polytexnic 0.5.0
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.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/.pull_requests/1371777257 +0 -0
- data/.pull_requests/1371927975 +0 -0
- data/.pull_requests/1372804345 +0 -0
- data/.pull_requests/1374784075 +0 -0
- data/.pull_requests/1375304853 +0 -0
- data/.pull_requests/1375408308 +0 -0
- data/.pull_requests/1375409462 +0 -0
- data/.pull_requests/1375410668 +0 -0
- data/.pull_requests/1375472132 +0 -0
- data/.pull_requests/1375485496 +0 -0
- data/.pull_requests/1375487548 +0 -0
- data/.pull_requests/1375492835 +0 -0
- data/.pull_requests/1375497765 +0 -0
- data/.pull_requests/1375559547 +0 -0
- data/.pull_requests/1375589063 +0 -0
- data/.pull_requests/1375841786 +0 -0
- data/.pull_requests/1376352634 +0 -0
- data/.pull_requests/1376353299 +0 -0
- data/.pull_requests/1376449284 +0 -0
- data/.pull_requests/1376452696 +0 -0
- data/.pull_requests/1376454166 +0 -0
- data/.pull_requests/1376532291 +0 -0
- data/.pull_requests/1376625487 +0 -0
- data/.pull_requests/1376690108 +0 -0
- data/.pull_requests/1376699046 +0 -0
- data/.pull_requests/1376707642 +0 -0
- data/.pull_requests/1377230284 +0 -0
- data/.pull_requests/1379118478 +0 -0
- data/.pull_requests/1379123150 +0 -0
- data/.pull_requests/1380221847 +0 -0
- data/.pull_requests/1380589654 +0 -0
- data/.pull_requests/1380673142 +0 -0
- data/.pull_requests/1380850800 +0 -0
- data/.pull_requests/1381001264 +0 -0
- data/.pull_requests/1381005204 +0 -0
- data/.pull_requests/1381103022 +0 -0
- data/.pull_requests/1381252832 +0 -0
- data/.pull_requests/1381276624 +0 -0
- data/.pull_requests/1381344234 +0 -0
- data/.pull_requests/1381385297 +0 -0
- data/.pull_requests/1381427498 +0 -0
- data/.pull_requests/1381429761 +0 -0
- data/.pull_requests/1381873684 +0 -0
- data/.pull_requests/1382045490 +0 -0
- data/.pull_requests/1382056384 +0 -0
- data/.pull_requests/1382405223 +0 -0
- data/.pull_requests/1382478400 +0 -0
- data/.pull_requests/1382479780 +0 -0
- data/.pull_requests/1382485483 +0 -0
- data/.pull_requests/1382569911 +0 -0
- data/.pull_requests/1382646199 +0 -0
- data/.pull_requests/1382649778 +0 -0
- data/.pull_requests/1382660987 +0 -0
- data/.pull_requests/1382743927 +0 -0
- data/.pull_requests/1382840347 +0 -0
- data/.pull_requests/1383077676 +0 -0
- data/.pull_requests/1383086948 +0 -0
- data/.pull_requests/1383161978 +0 -0
- data/.pull_requests/1383263695 +0 -0
- data/.pull_requests/1383274008 +0 -0
- data/.pull_requests/1383327328 +0 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +15 -0
- data/Guardfile +15 -0
- data/LICENSE.txt +22 -0
- data/README.md +21 -0
- data/Rakefile +2 -0
- data/lib/polytexnic/literal.rb +299 -0
- data/lib/polytexnic/postprocessor.rb +28 -0
- data/lib/polytexnic/postprocessors/html.rb +1139 -0
- data/lib/polytexnic/postprocessors/latex.rb +18 -0
- data/lib/polytexnic/postprocessors/polytex.rb +44 -0
- data/lib/polytexnic/preprocessor.rb +23 -0
- data/lib/polytexnic/preprocessors/html.rb +349 -0
- data/lib/polytexnic/preprocessors/latex.rb +43 -0
- data/lib/polytexnic/preprocessors/polytex.rb +127 -0
- data/lib/polytexnic/utils.rb +176 -0
- data/lib/polytexnic/version.rb +3 -0
- data/lib/polytexnic.rb +92 -0
- data/notes/pandoc.md +41 -0
- data/polytexnic.gemspec +28 -0
- data/polytexnic_commands.sty +5 -0
- data/precompiled_binaries/tralics +0 -0
- data/spec/fixtures/code_listing.tex +14 -0
- data/spec/fixtures/figures.tex +8 -0
- data/spec/fixtures/inline_math.html +4 -0
- data/spec/fixtures/inline_math.tex +3 -0
- data/spec/fixtures/math_environments.html +50 -0
- data/spec/fixtures/math_environments.tex +56 -0
- data/spec/fixtures/section_xrefs.tex +9 -0
- data/spec/fixtures/sidebar.tex +10 -0
- data/spec/fixtures/tables.tex +8 -0
- data/spec/fixtures/verbatim_environments.html +11 -0
- data/spec/fixtures/verbatim_environments.tex +13 -0
- data/spec/integration_spec.rb +34 -0
- data/spec/markdown_to_polytex_spec.rb +192 -0
- data/spec/resemble_matcher_spec.rb +69 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/support/resemble_matcher.rb +100 -0
- data/spec/to_html/asides_spec.rb +42 -0
- data/spec/to_html/chapters_and_sections_spec.rb +268 -0
- data/spec/to_html/characters_and_punctuation_spec.rb +138 -0
- data/spec/to_html/codelistings_spec.rb +70 -0
- data/spec/to_html/core_spec.rb +227 -0
- data/spec/to_html/eqref_spec.rb +32 -0
- data/spec/to_html/footnote_spec.rb +164 -0
- data/spec/to_html/graphics_and_figures_spec.rb +358 -0
- data/spec/to_html/lists_spec.rb +103 -0
- data/spec/to_html/literal_environments/code_spec.rb +141 -0
- data/spec/to_html/literal_environments/math_spec.rb +255 -0
- data/spec/to_html/literal_environments/unicode_spec.rb +12 -0
- data/spec/to_html/literal_environments/verbatim_spec.rb +168 -0
- data/spec/to_html/quotations_and_verse_spec.rb +86 -0
- data/spec/to_html/table_of_contents_spec.rb +93 -0
- data/spec/to_html/table_spec.rb +269 -0
- data/spec/to_html/text_formatting_spec.rb +50 -0
- data/spec/to_latex_spec.rb +197 -0
- data/tasks/bin/ruby_tests +41 -0
- data/tasks/run_tests_with_both_rubies.rake +5 -0
- data/tmp/.gitkeep +0 -0
- metadata +286 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c16ad8d5728f4ac2d87b81744b76c4a854e26d88
|
|
4
|
+
data.tar.gz: 5859a31e6722957809d2fd080c5170a05be7b5e1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 12e8c475ee247533ce1dcf0b1db8ad7cf3806a2cf81cf5726b935fbc0d23ac60ccc1998e7e38c5a234911be9ff370abdac44f6782caf5b5f1f28a3608240f3b2
|
|
7
|
+
data.tar.gz: 54b074f7a0df5e183cf333a52ceb5d4fa1b7addc3085c5b082da3c15f693a7afe03fba5295a8b140b85d48dfece737a8e74d258a021420935d497301007e584c
|
data/.gitignore
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
.bundle
|
|
4
|
+
.config
|
|
5
|
+
.yardoc
|
|
6
|
+
Gemfile.lock
|
|
7
|
+
InstalledFiles
|
|
8
|
+
_yardoc
|
|
9
|
+
coverage
|
|
10
|
+
doc/
|
|
11
|
+
lib/bundler/man
|
|
12
|
+
pkg
|
|
13
|
+
rdoc
|
|
14
|
+
spec/reports
|
|
15
|
+
test/tmp
|
|
16
|
+
test/version_tmp
|
|
17
|
+
tmp
|
|
18
|
+
log
|
|
19
|
+
.highlight_cache
|
|
20
|
+
|
|
21
|
+
# Compiled Object files
|
|
22
|
+
*.slo
|
|
23
|
+
*.lo
|
|
24
|
+
*.o
|
|
25
|
+
|
|
26
|
+
# Compiled Dynamic libraries
|
|
27
|
+
*.so
|
|
28
|
+
*.dylib
|
|
29
|
+
|
|
30
|
+
# Compiled Static libraries
|
|
31
|
+
*.lai
|
|
32
|
+
*.la
|
|
33
|
+
*.a
|
|
34
|
+
|
|
35
|
+
.DS_Store
|
|
36
|
+
.api_token
|
|
37
|
+
.project_id
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
polytexnic
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.0.0-p247
|
data/Gemfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
gemspec
|
|
4
|
+
|
|
5
|
+
group :test do
|
|
6
|
+
gem 'debugger2' unless RUBY_VERSION < "2.0"
|
|
7
|
+
gem 'coveralls', require: false
|
|
8
|
+
gem 'growl'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
group :development do
|
|
12
|
+
gem 'rspec', '~> 2.13'
|
|
13
|
+
gem 'guard-rspec'
|
|
14
|
+
gem 'rake'
|
|
15
|
+
end
|
data/Guardfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# A sample Guardfile
|
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
|
3
|
+
|
|
4
|
+
guard :rspec do
|
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
|
7
|
+
## Uncomment & edit below (and comment out above) to watch particular specs.
|
|
8
|
+
# watch(%r{^lib/(.+)\.rb$}) do
|
|
9
|
+
# [
|
|
10
|
+
# "spec/to_html/asides_spec.rb",
|
|
11
|
+
# "spec/to_html/codelistings_spec.rb"
|
|
12
|
+
# ]
|
|
13
|
+
# end
|
|
14
|
+
watch('spec/spec_helper.rb') { "spec" }
|
|
15
|
+
end
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Michael Hartl and Nick Merwin
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# polytexnic
|
|
2
|
+
|
|
3
|
+
This is the `polytexnic` gem, which converts input in Markdown or PolyTeX to HTML and LaTeX. It currently has no standalone applications, but rather exists to support the `polytexnic` gem.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
This gem is installed automatically as a dependency of the `polytexnic` gem:
|
|
8
|
+
|
|
9
|
+
$ gem install polytexnic
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
See [*The PolyTeXnic Book*](http://polytexnic.org/book) for more information.
|
|
14
|
+
|
|
15
|
+
## Contributing
|
|
16
|
+
|
|
17
|
+
1. Fork it
|
|
18
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
19
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
20
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
21
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
module Polytexnic
|
|
2
|
+
module Literal
|
|
3
|
+
|
|
4
|
+
# Matches the line for syntax highlighting.
|
|
5
|
+
# %= lang:<language>
|
|
6
|
+
LANG_REGEX = /^\s*%=\s+lang:\s*(\w+)/
|
|
7
|
+
|
|
8
|
+
# Matches the line for code inclusion.
|
|
9
|
+
# %= <</path/to/code.ext
|
|
10
|
+
CODE_INCLUSION_REGEX = /^\s*%=\s+<<\s*\( # opening
|
|
11
|
+
\s*([\w\/]+\.?(\w*)) # path
|
|
12
|
+
(?:,\s*lang:\s*(\w+))? # optional lang
|
|
13
|
+
\s*\) # closing paren
|
|
14
|
+
/x
|
|
15
|
+
|
|
16
|
+
# Makes the caches for literal environments.
|
|
17
|
+
def cache_literal(polytex, format = :html)
|
|
18
|
+
output = []
|
|
19
|
+
lines = polytex.split("\n")
|
|
20
|
+
cache_literal_environments(lines, output, format)
|
|
21
|
+
output.join("\n")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Handles environments that should be passed through the pipeline intact.
|
|
25
|
+
# The includes verbatim environments ('verbatim', 'Verbatim') and all the
|
|
26
|
+
# equation environments handled by MathJax ('equation', 'align', etc.).
|
|
27
|
+
# We take care to keep count of the number of begins we see so that the
|
|
28
|
+
# code handles nested environments correctly. I.e.,
|
|
29
|
+
# \begin{verbatim}
|
|
30
|
+
# \begin{verbatim}
|
|
31
|
+
# \emph{foo bar}
|
|
32
|
+
# \end{verbatim}
|
|
33
|
+
# \end{verbatim}
|
|
34
|
+
# lorem ipsum
|
|
35
|
+
# includes the internal literal text without stopping after the first
|
|
36
|
+
# \end{verbatim}.
|
|
37
|
+
#
|
|
38
|
+
# The control flow here is really nasty, but attempts to refactor it
|
|
39
|
+
# into a multi-pass solution have only resulted in even more complexity,
|
|
40
|
+
# and even then I've failed to get it to work. Thus, it shall for now
|
|
41
|
+
# follow the "ball of mud" pattern. (The only saving grace is that it's
|
|
42
|
+
# very thoroughly tested.)
|
|
43
|
+
def cache_literal_environments(lines, output, format)
|
|
44
|
+
latex = (format == :latex)
|
|
45
|
+
language = nil
|
|
46
|
+
in_verbatim = false
|
|
47
|
+
in_codelisting = false
|
|
48
|
+
while (line = lines.shift)
|
|
49
|
+
if line =~ LANG_REGEX && !in_verbatim
|
|
50
|
+
language = $1
|
|
51
|
+
elsif line =~ /\s*\\begin\{codelisting\}/ && !in_verbatim
|
|
52
|
+
in_codelisting = true
|
|
53
|
+
output << line
|
|
54
|
+
elsif line =~ /\s*\\end\{codelisting\}/ && !in_verbatim
|
|
55
|
+
in_codelisting = false
|
|
56
|
+
output << line
|
|
57
|
+
elsif line =~ CODE_INCLUSION_REGEX && !in_verbatim
|
|
58
|
+
# Reduce to a previously solved problem.
|
|
59
|
+
# We transform
|
|
60
|
+
# %= <<(/path/to/file.rb)
|
|
61
|
+
# to
|
|
62
|
+
# %= lang:rb
|
|
63
|
+
# \begin{code}
|
|
64
|
+
# <content of file.rb>
|
|
65
|
+
# \end{code}
|
|
66
|
+
# and then prepend the code to the current `lines` array.
|
|
67
|
+
filename = $1
|
|
68
|
+
if File.exist?(filename)
|
|
69
|
+
language = $3 || $2 || 'text'
|
|
70
|
+
code = ["%= lang:#{language}"]
|
|
71
|
+
code << '\begin{code}'
|
|
72
|
+
code.concat(File.read($1).split("\n"))
|
|
73
|
+
code << '\end{code}'
|
|
74
|
+
lines.unshift(*code)
|
|
75
|
+
else
|
|
76
|
+
lines.unshift("\\verb+ERROR: File '#{filename}' does not exist+")
|
|
77
|
+
end
|
|
78
|
+
elsif line.begin_literal?
|
|
79
|
+
in_verbatim = true
|
|
80
|
+
literal_type = line.literal_type
|
|
81
|
+
skip = line.math_environment? || latex
|
|
82
|
+
if line.math_environment? && !latex
|
|
83
|
+
output << '\begin{xmlelement*}{equation}'
|
|
84
|
+
output << '\begin{equation}'
|
|
85
|
+
end
|
|
86
|
+
math = line.math_environment?
|
|
87
|
+
label = nil
|
|
88
|
+
output << xmlelement(element(literal_type), skip) do
|
|
89
|
+
count = 1
|
|
90
|
+
text = []
|
|
91
|
+
text << line if line.math_environment? || (latex && !language)
|
|
92
|
+
while (line = lines.shift)
|
|
93
|
+
if line.begin_literal?(literal_type)
|
|
94
|
+
count += 1
|
|
95
|
+
elsif line.end_literal?(literal_type)
|
|
96
|
+
count -= 1
|
|
97
|
+
if count.zero?
|
|
98
|
+
in_verbatim = false
|
|
99
|
+
text << line if line.math_environment? || (latex && !language)
|
|
100
|
+
break
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
label = line if math && line =~ /^\s*\\label{.*?}\s*$/
|
|
104
|
+
text << line
|
|
105
|
+
end
|
|
106
|
+
raise "Missing \\end{#{line.literal_type}}" if count != 0
|
|
107
|
+
content = text.join("\n")
|
|
108
|
+
if math
|
|
109
|
+
key = digest(content)
|
|
110
|
+
literal_cache[key] = content
|
|
111
|
+
elsif language.nil?
|
|
112
|
+
key = digest(content)
|
|
113
|
+
literal_cache[key] = content
|
|
114
|
+
tag = 'literal'
|
|
115
|
+
else
|
|
116
|
+
format = latex ? 'latex' : 'html'
|
|
117
|
+
id = "#{content}--#{language}--#{format}--#{in_codelisting}"
|
|
118
|
+
key = digest(id, salt: code_salt)
|
|
119
|
+
code_cache[key] = [content, language, in_codelisting]
|
|
120
|
+
tag = 'code'
|
|
121
|
+
end
|
|
122
|
+
if latex || tag == 'code' || math
|
|
123
|
+
key
|
|
124
|
+
else
|
|
125
|
+
xmlelement(tag) { key }
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
if math && !latex
|
|
129
|
+
unless label.nil?
|
|
130
|
+
key = digest(label)
|
|
131
|
+
math_label_cache[key] = label
|
|
132
|
+
output << key
|
|
133
|
+
end
|
|
134
|
+
output << '\end{equation}'
|
|
135
|
+
unless label.nil?
|
|
136
|
+
string = label.scan(/\{(.*?)\}/).flatten.first
|
|
137
|
+
string = string.gsub(':', '-').gsub('_', underscore_digest)
|
|
138
|
+
output << "\\xbox{data-label}{#{string}}"
|
|
139
|
+
end
|
|
140
|
+
output << '\end{xmlelement*}'
|
|
141
|
+
end
|
|
142
|
+
language = nil
|
|
143
|
+
(output << '') unless latex # Force the next element to be a paragraph
|
|
144
|
+
else
|
|
145
|
+
output << line
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Returns a permanent salt syntax highlighting cache.
|
|
151
|
+
def code_salt
|
|
152
|
+
'fbbc13ed4a51e27608037365e1d27a5f992b6339'
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Caches both display and inline math.
|
|
156
|
+
def cache_display_inline_math(output)
|
|
157
|
+
output.tap do
|
|
158
|
+
cache_display_math(output)
|
|
159
|
+
cache_inline_math(output)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Caches display math.
|
|
164
|
+
# We support both TeX-style $$...$$ and LaTeX-style \[ ... \].
|
|
165
|
+
def cache_display_math(output)
|
|
166
|
+
output.gsub!(/\\\[(.*?)\\\]|\$\$(.*?)\$\$/m) do
|
|
167
|
+
math = "\\[ #{$1 || $2} \\]"
|
|
168
|
+
equation_element(math)
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Returns an equation element while caching the given content.
|
|
173
|
+
# We use this only for unnumbered, display equations, which requires using
|
|
174
|
+
# the `equation*` environment in place of `equation`.
|
|
175
|
+
def equation_element(content)
|
|
176
|
+
key = digest(content)
|
|
177
|
+
literal_cache[key] = content
|
|
178
|
+
"\\begin{xmlelement*}{equation}
|
|
179
|
+
\\begin{equation*}
|
|
180
|
+
#{key}
|
|
181
|
+
\\end{equation*}
|
|
182
|
+
\\end{xmlelement*}"
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Caches inline math.
|
|
186
|
+
# We support both TeX-style $...$ and LaTeX-style \( ... \).
|
|
187
|
+
# There's an annoying edge case involving literal dollar signs, as in \$.
|
|
188
|
+
# Handling it significantly complicates the regex, and necessesitates
|
|
189
|
+
# introducing an additional group to catch the character before the math
|
|
190
|
+
# dollar sign in $2 and prepend it to the inline math element.
|
|
191
|
+
def cache_inline_math(output)
|
|
192
|
+
output.gsub!(/(?:\\\((.*?)\\\)|([^\\]|^)\$(.*?[^\\])\$)/m) do
|
|
193
|
+
math = "\\( #{$1 || $3} \\)"
|
|
194
|
+
key = digest(math)
|
|
195
|
+
literal_cache[key] = math
|
|
196
|
+
$2.to_s + xmlelement('inline') { key }
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Converts references to hyperrefs.
|
|
201
|
+
# We want to convert
|
|
202
|
+
# Chapter~\ref{cha:foo}
|
|
203
|
+
# to
|
|
204
|
+
# \hyperref[cha:foo]{Chapter~\ref{cha:foo}
|
|
205
|
+
# which is then handled by LaTeX's hyperref package
|
|
206
|
+
# or by Tralics (where it converted to a link
|
|
207
|
+
# by the postprocessor).
|
|
208
|
+
# For completeness, we handle the case where the author neglects to
|
|
209
|
+
# use the nonbreak space ~.
|
|
210
|
+
def hyperrefs(string)
|
|
211
|
+
linked_item = "(Chapter|Section|Table|Box|Figure|Fig\.|Listing" +
|
|
212
|
+
"|Equation|Eq\.)"
|
|
213
|
+
ref = /(?:#{linked_item}(~| ))*(\\(?:eq)*ref){(.*?)}/i
|
|
214
|
+
string.gsub!(ref) do
|
|
215
|
+
"\\hyperref[#{$4}]{#{$1}#{$2}#{$3}{#{$4}}}"
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Handles non-ASCII Unicode characters.
|
|
220
|
+
# The Tralics part of the pipeline doesn't properly handle Unicode,
|
|
221
|
+
# which is odd since Tralics is a French project. Nevertheless,
|
|
222
|
+
# we can hack around the restriction by treating non-ASCII Unicode
|
|
223
|
+
# characters as literal elements and simply pass them through the
|
|
224
|
+
# pipeline intact.
|
|
225
|
+
def cache_unicode(string)
|
|
226
|
+
non_ascii_unicode = /([^\x00-\x7F]+)/
|
|
227
|
+
string.gsub(non_ascii_unicode) do
|
|
228
|
+
key = digest($1)
|
|
229
|
+
literal_cache[key] = $1
|
|
230
|
+
xmlelement('unicode') { key }
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def element(literal_type)
|
|
235
|
+
if math_environments.include?(literal_type)
|
|
236
|
+
'equation'
|
|
237
|
+
else
|
|
238
|
+
literal_type
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Returns supported math environments.
|
|
245
|
+
# Note that the custom AMS-TeX environments are supported
|
|
246
|
+
# in addition to the LaTeX defaults.
|
|
247
|
+
def math_environments
|
|
248
|
+
%w[align align*
|
|
249
|
+
eqnarray eqnarray* equation equation*
|
|
250
|
+
gather gather* gathered
|
|
251
|
+
multline multline*
|
|
252
|
+
]
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
class String
|
|
256
|
+
|
|
257
|
+
# Returns true if self matches \begin{...} where ... is a literal environment.
|
|
258
|
+
# Note: Support for the 'metacode' environment exists solely to allow
|
|
259
|
+
# meta-discussion of the 'code' environment.
|
|
260
|
+
def begin_literal?(literal_type = nil)
|
|
261
|
+
return false unless include?('\begin')
|
|
262
|
+
literal_type ||= "(?:verbatim|Verbatim|code|metacode|" +
|
|
263
|
+
"#{math_environment_regex})"
|
|
264
|
+
match(/^\s*\\begin{#{literal_type}}\s*$/)
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Returns true if self matches \end{...} where ... is a literal environment.
|
|
268
|
+
def end_literal?(literal_type)
|
|
269
|
+
return false unless include?('\end')
|
|
270
|
+
match(/^\s*\\end{#{Regexp.escape(literal_type)}}\s*$/)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Returns the type of literal environment.
|
|
274
|
+
# '\begin{verbatim}' => 'verbatim'
|
|
275
|
+
# '\begin{equation}' => 'equation'
|
|
276
|
+
# '\[' => 'display'
|
|
277
|
+
def literal_type
|
|
278
|
+
scan(/\\begin{(.*?)}/).flatten.first || 'display'
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Returns true if self begins a math environment.
|
|
282
|
+
def begin_math?
|
|
283
|
+
return false unless include?('\begin')
|
|
284
|
+
literal_type = "(?:#{math_environment_regex})"
|
|
285
|
+
match(/^\s*\\begin{#{literal_type}}\s*$/)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Returns true if self matches a valid math environment.
|
|
289
|
+
def math_environment?
|
|
290
|
+
match(/(?:#{math_environment_regex})/)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
private
|
|
294
|
+
|
|
295
|
+
# Returns a regex matching valid math environments.
|
|
296
|
+
def math_environment_regex
|
|
297
|
+
math_environments.map { |s| Regexp.escape(s) }.join('|')
|
|
298
|
+
end
|
|
299
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# encoding=utf-8
|
|
2
|
+
require 'cgi'
|
|
3
|
+
require 'polytexnic/postprocessors/html'
|
|
4
|
+
require 'polytexnic/postprocessors/latex'
|
|
5
|
+
require 'polytexnic/postprocessors/polytex'
|
|
6
|
+
|
|
7
|
+
module Polytexnic
|
|
8
|
+
module Postprocessor
|
|
9
|
+
include Html
|
|
10
|
+
include Latex
|
|
11
|
+
include Polytex
|
|
12
|
+
|
|
13
|
+
def postprocess(format)
|
|
14
|
+
case format
|
|
15
|
+
when :html
|
|
16
|
+
@html = xml_to_html(@xml)
|
|
17
|
+
when :latex
|
|
18
|
+
hyperrefs(@polytex)
|
|
19
|
+
raw_source = replace_hashes(@polytex)
|
|
20
|
+
@latex = highlight_source_code(raw_source)
|
|
21
|
+
when :polytex
|
|
22
|
+
remove_hypertarget
|
|
23
|
+
fix_verbatim_bug
|
|
24
|
+
write_polytex_code
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|