-
Notifications
You must be signed in to change notification settings - Fork 2
Support latest Plurimath UnitsML build with generated Opal payload #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suleman-uzair
wants to merge
5
commits into
main
Choose a base branch
from
feat/latest-plurimath-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
921aebe
Add UnitsML Opal payload generation
1c17cd2
test: removed released gems branches and updated plurimath submodule
53b343c
Update Plurimath Opal build setup
087c22a
test: updated regexes in OMML
ce297e2
Update Plurimath for Opal nil formatter fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: test | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| node-version: [16, 18, 20, 22] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
|
|
||
| - name: Install dependencies | ||
| run: npm install | ||
|
|
||
| - name: Build bundle | ||
| run: | | ||
| npm run podman:build | ||
| npm run podman:bundle | ||
|
|
||
| - name: Run JavaScript tests | ||
| run: npx jest | ||
|
suleman-uzair marked this conversation as resolved.
|
||
|
|
||
| - name: Run native Ruby tests | ||
| run: npm run test:rb | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "ox" | ||
| require "unitsdb" | ||
|
|
||
| module PlurimathJs | ||
| module UnitsmlOpalPayloadGenerator | ||
| module_function | ||
|
|
||
| def build_payload_hash(database_path: nil) | ||
| source_path = database_path || Unitsdb.data_dir | ||
| ::Unitsdb::Database.from_db(source_path).to_hash | ||
| end | ||
|
|
||
| def to_ruby_source(database_hash) | ||
| rendered_hash = database_hash.inspect | ||
|
|
||
| <<~RUBY | ||
| # frozen_string_literal: true | ||
|
|
||
| require "unitsml" | ||
| require "unitsml/unitsdb" | ||
|
|
||
| Unitsml::Unitsdb::Database.load_opal_payload( | ||
| #{rendered_hash}.freeze, | ||
| ) | ||
| RUBY | ||
| end | ||
|
|
||
| def write_to(file_path, database_hash: nil, database_path: nil) | ||
| payload_hash = database_hash || build_payload_hash(database_path: database_path) | ||
| absolute_path = File.expand_path(file_path) | ||
|
|
||
| File.write(absolute_path, to_ruby_source(payload_hash)) | ||
| absolute_path | ||
| end | ||
| end | ||
| end | ||
|
|
||
| if $PROGRAM_NAME == __FILE__ | ||
| output_path = ARGV[0] | ||
| database_path = ARGV[1] | ||
|
|
||
| abort "Usage: ruby scripts/generate_unitsml_opal_payload.rb OUTPUT_PATH [DATABASE_PATH]" unless output_path | ||
|
|
||
| PlurimathJs::UnitsmlOpalPayloadGenerator.write_to( | ||
| output_path, | ||
| database_path: database_path, | ||
| ) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
| import Plurimath from "../dist" | ||
|
|
||
| describe('to mathml with intent', () => { | ||
| it('', () => { | ||
| it('adds MathML intent metadata when requested', () => { | ||
| const math = new Plurimath('∑_𝑥^𝑦 𝑧', 'unicode') | ||
| const mathml = math.toMathml(true).trim() | ||
|
|
||
| expect(math.toMathml(true).trim()).toBe('<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">\n <mstyle displaystyle="true">\n <mrow intent=":sum(𝑥,𝑦,$naryand)">\n <munderover>\n <mo>∑</mo>\n <mi>𝑥</mi>\n <mi>𝑦</mi>\n </munderover>\n <mrow arg="naryand">\n <mi>𝑧</mi>\n </mrow>\n </mrow>\n </mstyle>\n</math>') | ||
| expect(mathml).toContain('intent=":sum(𝑥,𝑦,$naryand)"') | ||
| expect(mathml).toContain('<munderover>') | ||
| expect(mathml).toMatch(/<mo>(∑|∑)<\/mo>/) | ||
| expect(mathml).toMatch(/<mrow arg="naryand">\s*<mi>(𝑧|𝑧)<\/mi>\s*<\/mrow>/) | ||
| }) | ||
|
|
||
| it('omits MathML intent metadata by default', () => { | ||
| const math = new Plurimath('∑_𝑥^𝑦 𝑧', 'unicode') | ||
|
|
||
| expect(math.toMathml()).not.toContain(' intent=') | ||
| expect(math.toMathml()).not.toBe(math.toMathml(true)) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| import Plurimath from "../dist" | ||
|
|
||
| describe('to unicodemath', () => { | ||
| it('', () => { | ||
| it('matches conversion of UnicodeMath equation to UnicodeMath', () => { | ||
| const math = new Plurimath('∑_𝑥^𝑦 𝑧', 'unicode') | ||
|
|
||
| expect(math.toUnicodemath().trim()).toBe('∑_(𝑥)^(𝑦)▒〖𝑧〗') | ||
| }) | ||
|
|
||
| it('matches conversion of LaTeX equation to UnicodeMath', () => { | ||
| const math = new Plurimath('M \\= \\begin{bmatrix} \\-\\sin λ_0 \\& \\cos λ_0 \\& 0 \\\\ \\-\\sin φ_0 \\cos λ_0 \\& \\-\\sin φ_0 \\sin λ_0 \\& \\cos φ_0 \\\\ \\cos φ_0 \\cos λ_0 \\& \\cos φ_0 \\sin λ_0 \\& \\sin φ_0 \\end{bmatrix}', 'latex') | ||
|
|
||
| expect(math.toUnicodemath()).toBe('M = ⓢ(− sinλ_(0)&cosλ_(0)&0@− sinφ_(0)cosλ_(0)&− sinφ_(0)sinλ_(0)&cosφ_(0)@cosφ_(0)cosλ_(0)&cosφ_(0)sinλ_(0)&sinφ_(0))') | ||
| }) | ||
|
|
||
| it('tests basic equation performing plus operation', () => { | ||
| const math = new Plurimath('M \\= a \\+ b', 'latex') | ||
|
|
||
| expect(math.toUnicodemath()).toBe('M = a + b') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import Plurimath from "../dist" | ||
|
|
||
| describe('unitsml asciimath', () => { | ||
| it('converts simple unit: kg', () => { | ||
| const math = new Plurimath('"unitsml(kg)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('rm(kg)') | ||
| expect(math.toLatex()).toBe('\\mathrm{kg}') | ||
| expect(math.toMathml()).toMatch(/<mstyle mathvariant="normal">\s*<mi>kg<\/mi>\s*<\/mstyle>/) | ||
| }) | ||
|
|
||
| it('converts unit with value: 9.8 m/s^2', () => { | ||
| const math = new Plurimath('9.8 "unitsml(m/s^2)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('9.8 rm(m) * rm(s)^(- 2)') | ||
| expect(math.toLatex()).toBe('9.8 \\mathrm{m} \\cdot \\mathrm{s}^{- 2}') | ||
| }) | ||
|
|
||
| it('converts frequency unit: Hz', () => { | ||
| const math = new Plurimath('"unitsml(Hz)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('rm(Hz)') | ||
| expect(math.toLatex()).toBe('\\mathrm{Hz}') | ||
| }) | ||
|
|
||
| it('converts prefixed unit: MHz', () => { | ||
| const math = new Plurimath('"unitsml(MHz)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('rm(MHz)') | ||
| expect(math.toLatex()).toBe('\\mathrm{MHz}') | ||
| }) | ||
|
|
||
| it('converts compound unit: g/mol', () => { | ||
| const math = new Plurimath('"unitsml(g/mol)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('rm(g) * rm(mol)^(- 1)') | ||
| expect(math.toLatex()).toBe('\\mathrm{g} \\cdot \\mathrm{mol}^{- 1}') | ||
| }) | ||
|
|
||
| it('converts unit in expression context', () => { | ||
| const math = new Plurimath('1 "unitsml(A)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toBe('1 rm(A)') | ||
| expect(math.toLatex()).toBe('1 \\mathrm{A}') | ||
| }) | ||
|
|
||
| it('converts Hz in complex expression', () => { | ||
| const math = new Plurimath('540 xx 10^(12) "unitsml(Hz)"', 'asciimath') | ||
|
|
||
| expect(math.toAsciimath()).toContain('rm(Hz)') | ||
| expect(math.toLatex()).toContain('\\mathrm{Hz}') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "tmpdir" | ||
| require_relative "../scripts/generate_unitsml_opal_payload" | ||
|
|
||
| RSpec.describe PlurimathJs::UnitsmlOpalPayloadGenerator do | ||
| let(:database_hash) do | ||
| { | ||
| "schema_version" => "2.0.0", | ||
| "units" => [], | ||
| "prefixes" => [], | ||
| "quantities" => [], | ||
| "dimensions" => [], | ||
| "unit_systems" => [], | ||
| } | ||
| end | ||
|
|
||
| describe ".to_ruby_source" do | ||
| it "renders a Ruby payload file that loads the Opal database payload" do | ||
| source = described_class.to_ruby_source(database_hash) | ||
|
|
||
| expect(source).to include('require "unitsml"') | ||
| expect(source).to include("Unitsml::Unitsdb::Database.load_opal_payload(") | ||
| expect(source).not_to include("class OpalDatabase") | ||
| expect(source).not_to include("def self.from_db") | ||
| expect(source).to match(/"schema_version"\s*=>\s*"2\.0\.0"/) | ||
| expect(source).to include(".freeze") | ||
| end | ||
|
|
||
| it "renders unit symbols as a Ruby literal" do | ||
| source = described_class.to_ruby_source( | ||
| database_hash.merge( | ||
| "units" => [ | ||
| { | ||
| "id" => "ohm", | ||
| "symbols" => ["Ω", "µΩ"], | ||
| }, | ||
| ], | ||
| ), | ||
| ) | ||
|
|
||
| rendered_hash = source.match(/load_opal_payload\(\n (.*)\.freeze,/m)[1] | ||
| payload = eval(rendered_hash) # rubocop:disable Security/Eval | ||
|
|
||
| expect(payload["units"].first["symbols"]).to eq(["Ω", "µΩ"]) | ||
| end | ||
| end | ||
|
|
||
| describe ".write_to" do | ||
| it "writes the generated payload file to the requested path" do | ||
| file_path = nil | ||
|
|
||
| Dir.mktmpdir do |dir| | ||
| file_path = described_class.write_to( | ||
| File.join(dir, "unitsml_opal_payload.rb"), | ||
| database_hash: database_hash, | ||
| ) | ||
|
|
||
| generated_source = File.read(file_path) | ||
| expect(generated_source).to eq(described_class.to_ruby_source(database_hash)) | ||
| end | ||
|
|
||
| expect(file_path).to end_with("unitsml_opal_payload.rb") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import "./plurimath-opal.js"; | ||
|
|
||
| Opal.require("plurimath"); | ||
|
|
||
| export default class Plurimath { | ||
|
|
||
Submodule plurimath
updated
3215 files
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.