Add Repubmark::Elems::Power
This commit is contained in:
parent
af7e8cb8ab
commit
3a3bee771a
5 changed files with 85 additions and 18 deletions
|
@ -55,7 +55,52 @@ require_relative 'repubmark/elems/section'
|
|||
require_relative 'repubmark/elems/special'
|
||||
require_relative 'repubmark/elems/text'
|
||||
require_relative 'repubmark/elems/link'
|
||||
require_relative 'repubmark/elems/power'
|
||||
|
||||
module Repubmark
|
||||
FORMATS = %i[gemtext html summary_plain word_count].freeze
|
||||
|
||||
UNICODE_SUPS = {
|
||||
'0' => '⁰',
|
||||
'1' => '¹',
|
||||
'2' => '²',
|
||||
'3' => '³',
|
||||
'4' => '⁴',
|
||||
'5' => '⁵',
|
||||
'6' => '⁶',
|
||||
'7' => '⁷',
|
||||
'8' => '⁸',
|
||||
'9' => '⁹',
|
||||
'+' => '⁺',
|
||||
'-' => '⁻',
|
||||
'=' => '⁼',
|
||||
'(' => '⁽',
|
||||
')' => '⁾',
|
||||
}.freeze
|
||||
|
||||
UNICODE_SUBS = {
|
||||
'0' => '₀',
|
||||
'1' => '₁',
|
||||
'2' => '₂',
|
||||
'3' => '₃',
|
||||
'4' => '₄',
|
||||
'5' => '₅',
|
||||
'6' => '₆',
|
||||
'7' => '₇',
|
||||
'8' => '₈',
|
||||
'9' => '₉',
|
||||
'+' => '₊',
|
||||
'-' => '₋',
|
||||
'=' => '₌',
|
||||
'(' => '₍',
|
||||
')' => '₎',
|
||||
}.freeze
|
||||
|
||||
def self.unicode_sup(val)
|
||||
String(val).each_char.map { |chr| UNICODE_SUPS.fetch chr }.join.freeze
|
||||
end
|
||||
|
||||
def self.unicode_sub(val)
|
||||
String(val).each_char.map { |chr| UNICODE_SUBS.fetch chr }.join.freeze
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,9 +23,7 @@ module Repubmark
|
|||
|
||||
def to_summary_plain = "#@top/#@bottom".freeze
|
||||
|
||||
def to_html
|
||||
"<sup>#@top</sup>⁄<sub>#@bottom</sub>".freeze
|
||||
end
|
||||
def to_html = "<sup>#@top</sup>⁄<sub>#@bottom</sub>".freeze
|
||||
|
||||
def to_gemtext = "#@top/#@bottom".freeze
|
||||
end
|
||||
|
|
|
@ -73,6 +73,8 @@ module Repubmark
|
|||
|
||||
def mdash = base Special.new self, :mdash
|
||||
|
||||
def power(base, exponent) = base Power.new self, base, exponent
|
||||
|
||||
def quote(str = nil)
|
||||
quote = Quote.new self
|
||||
case [!!str, block_given?]
|
||||
|
@ -161,6 +163,8 @@ module Repubmark
|
|||
|
||||
def mdash(*args) = joint { |joint| joint.mdash(*args) }
|
||||
|
||||
def power(*args) = joint { |joint| joint.power(*args) }
|
||||
|
||||
def quote_italic(*args) = joint { |joint| joint.quote_italic(*args) }
|
||||
|
||||
def section(*args) = joint { |joint| joint.section(*args) }
|
||||
|
|
|
@ -5,19 +5,6 @@ module Repubmark
|
|||
class Note < Base
|
||||
parents :Joint
|
||||
|
||||
UNICODE_SUPS = {
|
||||
'1' => '¹',
|
||||
'2' => '²',
|
||||
'3' => '³',
|
||||
'4' => '⁴',
|
||||
'5' => '⁵',
|
||||
'6' => '⁶',
|
||||
'7' => '⁷',
|
||||
'8' => '⁸',
|
||||
'9' => '⁹',
|
||||
'0' => '⁰',
|
||||
}.freeze
|
||||
|
||||
def initialize(parent, index, anchor)
|
||||
super parent
|
||||
@index = index
|
||||
|
@ -43,8 +30,7 @@ module Repubmark
|
|||
private
|
||||
|
||||
def index_unicode_sup
|
||||
@index_unicode_sup ||=
|
||||
@index.to_s.each_char.map { |chr| UNICODE_SUPS.fetch chr }.join.freeze
|
||||
@index_unicode_sup ||= Repubmark.unicode_sup @index
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
34
lib/repubmark/elems/power.rb
Normal file
34
lib/repubmark/elems/power.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Repubmark
|
||||
module Elems
|
||||
class Power < Base
|
||||
parents :Joint
|
||||
|
||||
def initialize(parent, base, exponent)
|
||||
super parent
|
||||
|
||||
@base = Integer base
|
||||
@exponent = Integer exponent
|
||||
end
|
||||
|
||||
#################
|
||||
# Basic methods #
|
||||
#################
|
||||
|
||||
def word_count = 1
|
||||
|
||||
def to_summary_plain = "#@base#{unicode_exponent}".freeze
|
||||
|
||||
def to_html = "#@base<sup>#@exponent</sup>".freeze
|
||||
|
||||
def to_gemtext = "#@base#{unicode_exponent}".freeze
|
||||
|
||||
private
|
||||
|
||||
def unicode_exponent
|
||||
@unicode_exponent ||= Repubmark.unicode_sup @exponent
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue