Class: Jekyll::Converters::EuclidConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/jekyll/converters/euclid.rb

Overview

Converts .euclid files, which are a custom format for writing mathematical proofs based on latex math environments

Instance Method Summary collapse

Instance Method Details

#convert(content) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/jekyll/converters/euclid.rb', line 137

def convert(content)
  content = replace_theorems(content)
  content = replace_axioms(content)
  content = replace_definitions(content)
  content = replace_proofs(content)

  Jekyll::Converters::Markdown::KramdownParser.new(Jekyll.configuration).convert(content)
end

#get_term_index(content) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll/converters/euclid.rb', line 52

def get_term_index(content)
  theorem_index = content.index("\\theorem") || 999_999
  lemma_index = content.index("\\lemma") || 999_999
  corollary_index = content.index("\\corollary") || 999_999

  if theorem_index < lemma_index && theorem_index < corollary_index
    return theorem_index, "\\theorem"
  elsif lemma_index < corollary_index
    return lemma_index, "\\lemma"
  elsif corollary_index < 999_999
    return corollary_index, "\\corollary"
  end

  [-1, nil]
end

#matches(ext) ⇒ Object



10
11
12
# File 'lib/jekyll/converters/euclid.rb', line 10

def matches(ext)
  ext =~ /^\.euclid$/i
end

#output_ext(_) ⇒ Object



14
15
16
# File 'lib/jekyll/converters/euclid.rb', line 14

def output_ext(_)
  ".html"
end

#replace_axiom(content) ⇒ Object



36
37
38
39
40
# File 'lib/jekyll/converters/euclid.rb', line 36

def replace_axiom(content)
  @axiom_counter += 1

  "**Axiom #{@axiom_counter}:** *#{content[6..-6].strip}*"
end

#replace_axioms(content) ⇒ Object



123
124
125
126
# File 'lib/jekyll/converters/euclid.rb', line 123

def replace_axioms(content)
  @axiom_counter = 0
  replace_term("\\axiom", content, method(:replace_axiom))
end

#replace_corollary(content) ⇒ Object



30
31
32
33
34
# File 'lib/jekyll/converters/euclid.rb', line 30

def replace_corollary(content)
  @corollary_counter += 1

  "**Corollary #{@theorem_counter}.#{@corollary_counter}:** *#{content[10..-6].strip}*"
end

#replace_definition(content) ⇒ Object



42
43
44
45
46
# File 'lib/jekyll/converters/euclid.rb', line 42

def replace_definition(content)
  @definition_counter += 1

  "**Definition #{@definition_counter}:** *#{content[11..-6].strip}*"
end

#replace_definitions(content) ⇒ Object



128
129
130
131
# File 'lib/jekyll/converters/euclid.rb', line 128

def replace_definitions(content)
  @definition_counter = 0
  replace_term("\\definition", content, method(:replace_definition))
end

#replace_lemma(content) ⇒ Object



24
25
26
27
28
# File 'lib/jekyll/converters/euclid.rb', line 24

def replace_lemma(content)
  @theorem_counter += 1

  "**Lemma #{@theorem_counter}:** *#{content[6..-6].strip}*"
end

#replace_proof(content) ⇒ Object



48
49
50
# File 'lib/jekyll/converters/euclid.rb', line 48

def replace_proof(content)
  "*Proof.* #{content[7..-6]}"
end

#replace_proofs(content) ⇒ Object



133
134
135
# File 'lib/jekyll/converters/euclid.rb', line 133

def replace_proofs(content)
  replace_term("\\proof", content, method(:replace_proof))
end

#replace_term(term, content, replace_method) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jekyll/converters/euclid.rb', line 104

def replace_term(term, content, replace_method)
  term_index = content.index(term)
  end_index = content[term_index..].index("\\end") + term_index

  while term_index.positive?
    term_content = content[term_index..(end_index + 3)]
    content_before = content[0..(term_index - 1)]
    content_after = content[(end_index + 4)..]
    content = content_before + replace_method.call(term_content) + content_after
    term_index = content.index(term)

    term_index.nil? && break
    end_index = content[term_index..].index("\\end") + term_index

  end

  content
end

#replace_theorem(content) ⇒ Object



18
19
20
21
22
# File 'lib/jekyll/converters/euclid.rb', line 18

def replace_theorem(content)
  @theorem_counter += 1

  "**Theorem #{@theorem_counter}:** *#{content[8..-6].strip}*"
end

#replace_theorem_terms(content, term) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/jekyll/converters/euclid.rb', line 68

def replace_theorem_terms(content, term)
  case term
  when "\\theorem"
    replace_theorem(content)
  when "\\lemma"
    replace_lemma(content)
  when "\\corollary"
    replace_corollary(content)
  end
end

#replace_theorems(content) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jekyll/converters/euclid.rb', line 79

def replace_theorems(content)
  @theorem_counter = 0

  term_index, term = get_term_index(content)

  term != "\\corollary" && @corollary_counter = 0

  end_index = content[term_index..].index("\\end") + term_index

  while term_index.positive?
    term_content = content[term_index..(end_index + 3)]
    content_before = content[0..(term_index - 1)]
    content_after = content[(end_index + 4)..]
    content = content_before + replace_theorem_terms(term_content, term) + content_after

    term_index, term = get_term_index(content)

    term_index == -1 && break

    end_index = content[term_index..].index("\\end") + term_index
  end

  content
end