latex-decode 0.4.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7edb05e44fa7fbea8b75999b8f1c387022895e346bf1c366ed017d5a925349db
4
- data.tar.gz: e39f6f84999e606a164670ed88d4023e26ecb5bb71f1f6a1e7fc27700b38e34b
3
+ metadata.gz: b31903b54e1a793967de578a4fbeffed24cea678c9935c223408d133be0a5d5b
4
+ data.tar.gz: b23f75d41c74bc718ab390809f5cd15d7bf7e414c5aeab4d5c37dca9266b381a
5
5
  SHA512:
6
- metadata.gz: 9cf7b85e120dc609283cc3ed7783d941434ca6332323f3e3cfd370587c89c2add08d13cf5e85551f649f84e76e7e3c3392f48f97fcbdc8a5498cba6c1bfc2ae3
7
- data.tar.gz: 79ec1b585eb8e7d74f464b1002b6d71d6ad422da8bfc001b042da5e7ac07b317dcadc02511c1c37489acaec4bd399bc2cbed4c1fc5416c68c9679d0419674b9a
6
+ metadata.gz: 2f49ed520921abfc20d1928771f1a4cac58e5f8624e369f2f575e6e717edd2cc6d612fcc8240630e6a0fa641322b5504a14b9e97d50398b190419cee62e26698
7
+ data.tar.gz: 1e87d5da7b85480ffa54e2604f6fe0633061a466fbe3ad588d2a45506eda81123594a5463bbeee2676d7f2c8a1d82a744a1076f1d304881dc1b140f70579e2c7
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  LaTeX::Decode
2
2
  =============
3
- [![ci](https://github.com/inukshuk/latex-decode/actions/workflows/ci.yml/badge.svg)](https://github.com/inukshuk/latex-decode/actions/workflows/ci.yml)
4
- [![coverage](https://coveralls.io/repos/github/inukshuk/latex-decode/badge.svg?branch=main)](https://coveralls.io/github/inukshuk/latex-decode?branch=main)
3
+ [![CI](https://github.com/inukshuk/latex-decode/actions/workflows/ci.yml/badge.svg)](https://github.com/inukshuk/latex-decode/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/latex-decode.svg)](https://badge.fury.io/rb/latex-decode)
5
5
 
6
6
  LaTeX::Decode is a Ruby gem to convert LaTeX input to Unicode.
7
7
  Its original use was as an input filter for [BibTeX-Ruby][]
@@ -11,16 +11,48 @@ are based on François Charette's equivalent Perl module [LaTeX::Decode][].
11
11
 
12
12
  Quickstart
13
13
  ----------
14
- $ [sudo] gem install latex-decode
14
+ $ gem install latex-decode
15
15
  $ irb
16
16
  >> require 'latex/decode'
17
17
  >> LaTeX.decode "dipl\\^{o}me d'\\'{e}tudes sup\\'erieures"
18
18
  => "diplôme d'études supérieures"
19
19
 
20
+ Math
21
+ ----
22
+ Math in `$…$`, `\(…\)`, `$$…$$` or `\[…\]`
23
+ is converted to Unicode if all of it can be represented,
24
+ which covers most math found in bibliographies:
25
+ Greek letters and symbols, sub- and superscripts,
26
+ font letters like `\mathbb`, accents, fractions and roots.
27
+
28
+ >> LaTeX.decode '$\alpha$-decay of $^{13}$C'
29
+ => "α-decay of ¹³C"
30
+ >> LaTeX.decode 'Sorting in $O(n \log n)$ on $\mathbb{R}^n$'
31
+ => "Sorting in O(n log n) on ℝⁿ"
32
+
33
+ Math that cannot be converted completely is kept as it is,
34
+ including the delimiters,
35
+ so that it can still be rendered,
36
+ for example, by MathJax or KaTeX.
37
+ Unicode has no subscript f, for instance:
38
+
39
+ >> LaTeX.decode 'Effective temperature $T_{\mathrm{eff}}$'
40
+ => "Effective temperature $T_{\\mathrm{eff}}$"
41
+
42
+ To keep all math as it is always, pass `math: false`:
43
+
44
+ >> LaTeX.decode '$\alpha$-decay', math: false
45
+ => "$\\alpha$-decay"
46
+
47
+ Math nested in text inside math,
48
+ like `$a \text{ if $b$} c$`,
49
+ is not currently supported.
50
+
20
51
 
21
52
  Issues
22
53
  ------
23
- Please use the tracker of the project's [repository][] to report any issues.
54
+ Please use the tracker of the project's [repository][]
55
+ to report any issues.
24
56
  When describing intended behaviour,
25
57
  please use the syntax of the Cucumber features used by LaTeX::Decode.
26
58
  For instance, you could describe the example above as:
@@ -36,13 +68,14 @@ For instance, you could describe the example above as:
36
68
 
37
69
  Credits
38
70
  -------
39
- Kudos and thanks to all [contributors][] who have made LaTeX::Decode possible!
71
+ Kudos and thanks to all [contributors][]
72
+ who have made LaTeX::Decode possible!
40
73
 
41
- Copyright (C) 2011-2022 Sylvester Keil
74
+ Copyright (C) 2011-2026 Sylvester Keil\
42
75
  Copyright (C) 2010 François Charette
43
76
 
44
77
 
45
- [BibTeXRuby]: http://rubygems.org/gems/bibtex-ruby
78
+ [BibTeX-Ruby]: https://rubygems.org/gems/bibtex-ruby
46
79
  [LaTeX::Decode]: https://github.com/fc7/LaTeX-Decode
47
80
 
48
81
  [repository]: https://github.com/inukshuk/latex-decode
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module LaTeX
4
4
  module Decode
@@ -13,21 +13,11 @@ module LaTeX
13
13
  . \u0307
14
14
  '' \u0308
15
15
  " \u0308
16
- }.map { |s| LaTeX.to_unicode(s) }].freeze
16
+ }].freeze
17
17
 
18
18
  @patterns = [
19
- ruby_18 {
20
- /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })\{([[:alpha:]]+)\}/ou
21
- } ||
22
- ruby_19 {
23
- /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })\{(\p{L}\p{M}*)\}/ou
24
- },
25
- ruby_18 {
26
- /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })([[:alpha:]])/ou
27
- } ||
28
- ruby_19 {
29
- /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })(\p{L}\p{M}*)/ou
30
- }
19
+ /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })\{(\p{L}\p{M}*)\}/ou,
20
+ /\\(#{ map.keys.map { |k| Regexp.escape(k) }.join('|') })(\p{L}\p{M}*)/ou
31
21
  ].freeze
32
22
 
33
23
  end
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module LaTeX
4
4
  module Decode
@@ -11,24 +11,21 @@ module LaTeX
11
11
  name.split('::').last.downcase.to_sym
12
12
  end
13
13
 
14
- def inherited(base)
15
- subclasses << base
16
- end
17
-
18
- def subclasses
19
- @subclasses ||= []
20
- end
21
-
22
14
  def decode(string)
23
15
  decode!(string.dup)
24
16
  end
25
17
 
26
18
  def decode!(string)
27
19
  patterns.each do |pattern|
28
- string.gsub!(pattern) { |m| [$2,map[$1],$3].compact.join }
20
+ decode_pattern!(string, pattern)
29
21
  end
30
22
  string
31
23
  end
24
+
25
+ def decode_pattern!(string, pattern)
26
+ string.gsub!(pattern) { |m| [$2,map[$1],$3].compact.join }
27
+ string
28
+ end
32
29
  end
33
30
  end
34
31
 
@@ -49,7 +46,7 @@ module LaTeX
49
46
  string.gsub!(/(\\[^\sij&#\$\{\}_~%])\s+([[:alpha:]]+)\b/i, '\1{\2}')
50
47
 
51
48
  # non-breaking spaces
52
- string.gsub!(/(\A|[^\\])~/, LaTeX.to_unicode("\\1\u00a0"))
49
+ string.gsub!(/(\A|[^\\])~/, "\\1\u00a0")
53
50
 
54
51
  string
55
52
  end
@@ -1,26 +1,4 @@
1
-
2
- if RUBY_VERSION < "1.9"
3
- $KCODE = 'U'
4
-
5
- module LaTeX
6
- def self.to_unicode(string)
7
- string.gsub(/\\?u([\da-f]{4})/i) { |m| [$1.to_i(16)].pack('U') }
8
- end
9
- end
10
-
11
- def ruby_18; yield; end
12
- def ruby_19; false; end
13
- else
14
-
15
- module LaTeX
16
- def self.to_unicode(string)
17
- string
18
- end
19
- end
20
-
21
- def ruby_18; false; end
22
- def ruby_19; yield; end
23
- end
1
+ # frozen_string_literal: true
24
2
 
25
3
  if RUBY_PLATFORM == 'java'
26
4
  require 'java'
@@ -33,65 +11,9 @@ if RUBY_PLATFORM == 'java'
33
11
  end
34
12
 
35
13
  else
36
- if RUBY_VERSION >= '2.3'
37
- module LaTeX
38
- def self.normalize_C(string)
39
- string.unicode_normalize(:nfc)
40
- end
41
- end
42
- else
43
- begin
44
- require 'unicode'
45
-
46
- # Use the Unicode gem
47
- module LaTeX
48
- def self.normalize_C(string)
49
- Unicode::normalize_C(string)
50
- end
51
- end
52
- rescue LoadError
53
- begin
54
- require 'active_support/multibyte/chars'
55
-
56
- # Use ActiveSupport's normalizer
57
- module LaTeX
58
- def self.normalize_C(string)
59
- ActiveSupport::Multibyte::Chars.new(string).normalize(:c).to_s
60
- end
61
- end
62
- rescue LoadError
63
- fail "Failed to load unicode normalizer: please gem install unicode (or active_support)"
64
- end
65
- end
66
- end
67
- end
68
-
69
- module LaTeX
70
- begin
71
- require 'ritex'
72
-
73
- def self.ritex
74
- Ritex::Parser.new(:mathml)
75
- end
76
-
77
- def self.to_math_ml(string)
78
- ritex.parse string, :nowrap => true, :display => false
79
- end
80
-
81
- rescue LoadError
82
- begin
83
- require 'math_ml'
84
-
85
- def self.to_math_ml(string)
86
- MathML::String.mathml_latex_parser.parse(string, false)
87
- end
88
-
89
- rescue LoadError
90
- # No MathML conversion
91
-
92
- def self.to_math_ml(string)
93
- string
94
- end
14
+ module LaTeX
15
+ def self.normalize_C(string)
16
+ string.unicode_normalize(:nfc)
95
17
  end
96
18
  end
97
19
  end
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module LaTeX
4
4
  module Decode
@@ -17,7 +17,7 @@ module LaTeX
17
17
  b \u0331
18
18
  B \u0335
19
19
  t \u0361
20
- }.map { |s| LaTeX.to_unicode(s) }].freeze
20
+ }].freeze
21
21
 
22
22
  @map = @macros.merge(Hash[*%w{
23
23
  l ł
@@ -31,12 +31,7 @@ module LaTeX
31
31
  }]).freeze
32
32
 
33
33
  @patterns = [
34
- ruby_18 {
35
- /\\(#{ @macros.keys.map { |k| Regexp.escape(k) }.join('|') })\{([[:alpha:]]?)([[:alpha:]]*)\}/ou
36
- } ||
37
- ruby_19 {
38
- /\\(#{ @macros.keys.map { |k| Regexp.escape(k) }.join('|') })\{(\p{L}\p{M}*)([[:alpha:]]*)\}/ou
39
- },
34
+ /\\(#{ @macros.keys.map { |k| Regexp.escape(k) }.join('|') })\{(\p{L}\p{M}*)([[:alpha:]]*)\}/ou,
40
35
  /\\(o|O|l|L|aa|AA|ae|AE)\b/,
41
36
  ].freeze
42
37
 
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module LaTeX
4
4
  module Decode
@@ -19,6 +19,7 @@ module LaTeX
19
19
  mu μ
20
20
  nu ν
21
21
  xi ξ
22
+ pi π
22
23
  rho ρ
23
24
  sigma σ
24
25
  tau τ
@@ -41,6 +42,7 @@ module LaTeX
41
42
  Mu Μ
42
43
  Nu Ν
43
44
  Xi Ξ
45
+ Pi Π
44
46
  Rho Ρ
45
47
  Sigma Σ
46
48
  Tau Τ