From 3a3bee771a42cff3836273a98b5d2dcd2e2e37f3 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 6 Mar 2024 21:59:25 +0400 Subject: [PATCH] Add Repubmark::Elems::Power --- lib/repubmark.rb | 45 +++++++++++++++++++++++++++++++++ lib/repubmark/elems/fraction.rb | 4 +-- lib/repubmark/elems/joint.rb | 4 +++ lib/repubmark/elems/note.rb | 16 +----------- lib/repubmark/elems/power.rb | 34 +++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 lib/repubmark/elems/power.rb diff --git a/lib/repubmark.rb b/lib/repubmark.rb index ff58ca7..dd6ce0a 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -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 diff --git a/lib/repubmark/elems/fraction.rb b/lib/repubmark/elems/fraction.rb index f78d1f1..13e98d3 100644 --- a/lib/repubmark/elems/fraction.rb +++ b/lib/repubmark/elems/fraction.rb @@ -23,9 +23,7 @@ module Repubmark def to_summary_plain = "#@top/#@bottom".freeze - def to_html - "#@top#@bottom".freeze - end + def to_html = "#@top#@bottom".freeze def to_gemtext = "#@top/#@bottom".freeze end diff --git a/lib/repubmark/elems/joint.rb b/lib/repubmark/elems/joint.rb index ea21bad..5b11672 100644 --- a/lib/repubmark/elems/joint.rb +++ b/lib/repubmark/elems/joint.rb @@ -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) } diff --git a/lib/repubmark/elems/note.rb b/lib/repubmark/elems/note.rb index 9d268db..1f4ef45 100644 --- a/lib/repubmark/elems/note.rb +++ b/lib/repubmark/elems/note.rb @@ -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 diff --git a/lib/repubmark/elems/power.rb b/lib/repubmark/elems/power.rb new file mode 100644 index 0000000..2d4f857 --- /dev/null +++ b/lib/repubmark/elems/power.rb @@ -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#@exponent".freeze + + def to_gemtext = "#@base#{unicode_exponent}".freeze + + private + + def unicode_exponent + @unicode_exponent ||= Repubmark.unicode_sup @exponent + end + end + end +end