Skip to content

Commit 1340854

Browse files
committed
Add property html to Dimensionality
1 parent 39d9b4a commit 1340854

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

quantities/dimensionality.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def unicode(self):
5252
def latex(self):
5353
return markup.format_units_latex(self)
5454

55+
@property
56+
def html(self):
57+
return markup.format_units_html(self)
58+
5559
def __hash__(self):
5660
res = hash(unit_registry['dimensionless'])
5761
for key in sorted(self.keys(), key=operator.attrgetter('format_order')):

quantities/markup.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,27 @@ def format_units_unicode(udict):
8989
def format_units_latex(udict,font='mathrm',mult=r'\cdot',paren=False):
9090
'''
9191
Replace the units string provided with an equivalent latex string.
92-
92+
9393
Division (a/b) will be replaced by \frac{a}{b}.
94-
94+
9595
Exponentiation (m**2) will be replaced with superscripts (m^{2})
96-
97-
The latex is set with the font argument, and the default is the normal,
98-
non-italicized font mathrm. Other useful options include 'mathnormal',
96+
97+
The latex is set with the font argument, and the default is the normal,
98+
non-italicized font mathrm. Other useful options include 'mathnormal',
9999
'mathit', 'mathsf', and 'mathtt'.
100-
100+
101101
Multiplication (*) are replaced with the symbol specified by the mult argument.
102102
By default this is the latex \cdot symbol. Other useful
103103
options may be '' or '*'.
104-
104+
105105
If paren=True, encapsulate the string in '\left(' and '\right)'
106-
106+
107107
The result of format_units_latex is encapsulated in $. This allows the result
108108
to be used directly in Latex in normal text mode, or in Matplotlib text via the
109109
MathText feature.
110-
110+
111111
Restrictions:
112-
This routine will not put CompoundUnits into a fractional form.
112+
This routine will not put CompoundUnits into a fractional form.
113113
'''
114114
res = format_units(udict)
115115
if res.startswith('(') and res.endswith(')'):
@@ -128,3 +128,36 @@ def format_units_latex(udict,font='mathrm',mult=r'\cdot',paren=False):
128128
res = r'\left(%s\right)' % res
129129
res = r'$\%s{%s}$' % (font,res)
130130
return res
131+
132+
133+
def format_units_html(udict,font='%s',mult=r'⋅',paren=False):
134+
'''
135+
Replace the units string provided with an equivalent html string.
136+
137+
Exponentiation (m**2) will be replaced with superscripts (m<sup>2</sup>})
138+
139+
No formating is done, change `font` argument to e.g.:
140+
'<span style="color: #0000a0">%s</span>' to have text be colored blue.
141+
142+
Multiplication (*) are replaced with the symbol specified by the mult
143+
argument. By default this is the latex &sdot; symbol. Other useful options
144+
may be '' or '*'.
145+
146+
If paren=True, encapsulate the string in '(' and ')'
147+
148+
'''
149+
res = format_units(udict)
150+
if res.startswith('(') and res.endswith(')'):
151+
# Compound Unit
152+
compound = True
153+
else:
154+
# Not a compound unit
155+
compound = False
156+
# Replace exponentiation (**exp) with ^{exp}
157+
res = re.sub(r'\*{2,2}(?P<exp>\d+)',r'<sup>\g<exp></sup>',res)
158+
# Remove multiplication signs
159+
res = re.sub(r'\*',mult,res)
160+
if paren and not compound:
161+
res = '(%s)' % res
162+
res = font % res
163+
return res

quantities/tests/test_dimensionality.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
joule_str = 'kg*m**2/s**2'
1515
joule_uni = 'kg·m²/s²'
1616
joule_tex = r'$\mathrm{\frac{kg{\cdot}m^{2}}{s^{2}}}$'
17+
joule_htm = 'kg&sdot;m<sup>2</sup>/s<sup>2</sup>'
1718
Joule = Dimensionality({pq.J: 1})
1819
Joule_str = 'J'
1920

@@ -24,6 +25,7 @@ def test_dimensionality_str(self):
2425
self.assertEqual(joule.string, joule_str)
2526
self.assertEqual(joule.unicode, joule_uni)
2627
self.assertEqual(joule.latex, joule_tex)
28+
self.assertEqual(joule.html, joule_htm)
2729
self.assertEqual(Joule.string, 'J')
2830

2931
def test_equality(self):

0 commit comments

Comments
 (0)