@latex.html


001| """LaTeX Formula Database.
001| 
001| Commands:
001|     /1      - Get an equation image from Google
001|     /2      - Get an equation image from WordPress
001|     @1      - Get ALL equation images (from Google)
001|     @2      - Get ALL equation images (from WordPres)
001|     html    - Generate LaTeX Equations HTML page
001|     test    - Test
001| 
001| Data:
001|     Eqs     - Dictionary of LaTeX equations
001|     EqNames - List of dictionary keys
001|     Notes   - For the equations
001| 
001| Functions:
001|     make_webpage    (fname, fpath)
001|     latex_maker     (url, fname, fpath)
001|     latex_maker_1   (eq_name, size, fname)
001|     latex_maker_2   (eq_name, size, fg, bg, fname)
001|     latex_maker_3   (eq_name)
001|     latex_unquote   (url)
001|     old_latex_google (eq_name, size, fpath)
001| 
001| TODO: Add Captions and Descriptions.
001| """

002| ####################################################################################################
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from urllib.parse import quote,unquote
007| from urllib.parse import quote_plus, unquote_plus
008| from logger import logger,info,debug,trace
009| from goodies import KWARG
010| from webpage import WebPage
011| from htget import htget
012| ####################################################################################################
013| BasePath = r'C:\CJS\prj\Python\app'
014| HtmlPath = r'C:\CJS\www\root\lib\eqs'
015| Log = logger('latex')
016| 
017| CMND = 'html'
018| ARGS = {
019|     'eq': '_',
020|     'sz': '2',
021|     #'fname': 'img_%s.png',
022|     #'fpath': BasePath,
023| }
024| 
025| ##------------------------------------------------------------------------------------------------##
026| UrlGoogle = r'http://chart.apis.google.com/chart?cht=tx&chs=%d&chl=%s'
027| UrlWordPress = r'https://s0.wp.com/latex.php?latex=%s&s=%d&fg=%s&bg=%s'
028| 
029| ##------------------------------------------------------------------------------------------------##
030| Eqs = {
031| '_': r"x",
032| 
033| 'Pythag1': r'\displaystyle{d}=\sqrt{{x^2}+{y^2}}',
034| 'Pythag2': r'\displaystyle{d}=\sqrt{{x^2}+{y^2}+{z^2}}',
035| 'Pythag3': r'\displaystyle{d}=\sqrt{\sum^{n}_{i}\;{x}_{i}^{2}}',
036| 
037| 'Euler': r"\displaystyle{e}^{{i}\pi}-1=0",
038| 
039| 'complex1': r"\displaystyle{z}=(a+bi)=\eta(\cos\theta+{i}\sin\theta)=\eta{e}^{i\theta}",
040| 'complex2': r"\displaystyle\eta=|z|=\sqrt{a^2+b^2},\;\;\;\theta=\arg(z)=\arctan\frac{a}{b}",
041| 
042| 'sine1': r"\displaystyle\Lambda=\eta{e}^{{i}2\pi\kappa{x}-\omega\sigma}",
043| 'sine2': r"\displaystyle\Lambda=\eta_1{e}^{{i}2\pi\kappa_1{x}}+\eta_2{e}^{{i}2\pi\kappa_2{x}}+\eta_3{e}^{{i}2\pi\kappa_3{x}}+\ldots",
044| 
045| 'quadratic':r"\displaystyle\frac{{-b}\pm\sqrt{{b^2}-{4ac}}}{2a}",
046| 
047| 'product': r"\displaystyle\frac{d}{dx}\left[f(x)\,g(x)\right]=f(x)\,\frac{d}{dx}\left[g(x)\right]+\frac{d}{dx}\left[f(x)\right]\,g(x)",
048| 'chain': r"\displaystyle\frac{d}{dx}\left[f(g(x))\right]=\frac{d}{du}\left[f(u)\right]\,\frac{d}{dx}\left[g(x)\right]",
049| 
050| 'Lorentz': r"\displaystyle\gamma=\frac{1}{\sqrt{1-\left(\frac{v}{c}\right)^2}}",
051| 'SR1': r"\displaystyle{E}={mc}^{2}",
052| 'SR2': r"\displaystyle{E}=\sqrt{(mc^{2})^{2}+(pc)^{2}}",
053| 'GR': r"\displaystyle{G}_{\mu\nu}+\Lambda{g}_{\mu\nu}=\frac{8\pi{G}}{c^4}T_{\mu\nu}",
054| 
055| 'Q1': r"\displaystyle|\Psi\rangle=|0\rangle+|1\rangle",
056| 'Q2': r"\displaystyle|\Psi\rangle=\frac{1}{\sqrt{2}}\left(|0\rangle+|1\rangle\right)",
057| 'Q3': r"\displaystyle|\Psi\rangle=\frac{|0\rangle}{\sqrt{2}}+\frac{|1\rangle}{\sqrt{2}}",
058| 
059| 'Hamilton': r"\displaystyle\hat{H}=\hat{T}\!+\!\hat{V}=\frac{\hbar^2}{2m}(\frac{\partial^2}{\partial x^2}+\frac{\partial^2}{\partial y^2}+\frac{\partial^2}{\partial z^2})+V(\bold{r},t)",
060| 
061| 'Schrod1': r"\displaystyle{i}\hbar\frac{d}{dt}\,\Psi(x,\!t)=\hat{H}\,\Psi(x,\!t)",
062| 'Schrod2': r"\displaystyle{i}\hbar\frac{\partial}{\partial{t}}\,\Psi(x,\!t)=\!\left[\!-\frac{\hbar^2}{2m}\,\frac{\partial^2}{\partial{x}^2}+\!V(x,\!t)\right]\!\!\Psi(x,\!t)",
063| 
064| 'entropyB': r"\displaystyle{S}_\text{Boltzmann}={K}_{B}\log\Omega",
065| 'entropyS': r"\displaystyle{S}_\text{Shannon}=\sum_{i}\,\text{P}\!\left({x}_{i}\right)\,\log_{2}\text{P}\!\left({x}_{i}\right)",
066| 
067| 'nabla': r"\displaystyle\nabla=\hat{x}\frac{\partial}{\partial{x}}+\hat{y}\frac{\partial}{\partial{y}}+\hat{z}\frac{\partial}{\partial{z}}",
068| 'nabla2': r"\displaystyle\nabla^2=\hat{x}\frac{\partial^2}{\partial{x^2}}+\hat{y}\frac{\partial^2}{\partial{y^2}}+\hat{z}\frac{\partial^2}{\partial{z^2}}",
069| 
070| 'diffusion': r"\displaystyle\frac{\partial}{\partial{t}}\,{T}(x,\!t)={D}\frac{\partial^{2}}{\partial{x}^{2}}\,{T}(x,\!t)",
071| 
072| 'Gauss1': r"\displaystyle{g}(x)={ae}^{-\left(\frac{x}{c}\right)^2}",
073| 'Gauss1dx': r"\displaystyle{g}'\!(x)=\frac{d}{dx}\,{g}(x)=\frac{-2ax}{c^2}\;{ae}^{-\left(\frac{x}{c}\right)^2}",
074| 'Gauss1dx2': r"\displaystyle{g}''\!(x)=\frac{d^2}{dx^2}\,{g}(x)=\frac{-2ac^2+4ax^2}{c^4}\;{ae}^{-\left(\frac{x}{c}\right)^2}",
075| 'Gauss1dx3': r"\displaystyle{g}'''\!(x)=\frac{d^3}{dx^3}\,{g}(x)=\frac{-8ax^{3}+12ac^{2}x}{c^6}\;{ae}^{-\left(\frac{x}{c}\right)^2}",
076| 
077| 'Gauss2': r"\displaystyle{g}(x,y)={ae}^{-\left(\frac{x^2+y^2}{c^2}\right)}",
078| 'Gauss2dx': r"\displaystyle{g}_x'(x,y)=\frac{\partial}{\partial{x}}{ae}^{-\left(\frac{x^2+y^2}{c^2}\right)}=\frac{-2ax}{c^2}\;{e}^{-\left(\frac{x^2+y^2}{c^2}\right)}",
079| 'Gauss2dy': r"\displaystyle{g}_y'(x,y)=\frac{\partial}{\partial{y}}{ae}^{-\left(\frac{x^2+y^2}{c^2}\right)}=\frac{-2ay}{c^2}\;{e}^{-\left(\frac{x^2+y^2}{c^2}\right)}",
080| 'Gauss2dx2': r"\displaystyle{g}_x''(x,y)=\frac{\partial^2}{\partial{x^2}}{ae}^{-\left(\frac{x^2+y^2}{c^2}\right)}=\frac{4ax^2-2ac^2}{c^4}\;{e}^{-\left(\frac{x^2+y^2}{c^2}\right)}",
081| 'Gauss2dy2': r"\displaystyle{g}_y''(x,y)=\frac{\partial^2}{\partial{y^2}}{ae}^{-\left(\frac{x^2+y^2}{c^2}\right)}=\frac{4ay^2-2ac^2}{c^4}\;{e}^{-\left(\frac{x^2+y^2}{c^2}\right)}",
082| 
083| 'Expon0': r"\displaystyle\lim_{n\rightarrow\infty}\left(1+\frac{1}{n}\right)^{n}",
084| 'Expon1': r"\displaystyle\exp(x)=\sum_{n=0}^{\infty}\frac{\;\;x^n}{n!}",
085| 'Expon2': r"\displaystyle\exp(x)=\frac{x^0}{0!}+\frac{x^1}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+\frac{x^4}{4!}\cdots",
086| 'Expon3': r"\displaystyle\exp(x)=1+x+\frac{x^2}{2!}+\frac{x^3}{3!}+\frac{x^4}{4!}\cdots",
087| 'Expon4': r"\displaystyle\exp(x)=1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}\cdots",
088| 
089| 'orb.period': r"\displaystyle{T}={2}\pi\sqrt{\frac{{a}^{3}}{GM}}",
090| 'orb.speed1': r"\displaystyle{v}\approx\sqrt{\frac{GM}{a}}\;\;\;\textsf{note:}\;{GM}=\mu",
091| 'orb.speed2': r"\displaystyle{v}\approx\frac{{2}\pi{a}}{T}",
092| 'orb.radius': r"\displaystyle{a}=\sqrt[3]{\frac{GMT^2}{{4}\pi^{2}}}",
093| 'esc.speed' : r"\displaystyle{v}_{e}=\sqrt{\frac{{2}GM}{d}}",
094| 'surf.grav' : r"\displaystyle{g}=\frac{GM}{{r}^{2}}",
095| 
096| 'rot1': r'\displaystyle{r}=\sqrt{(x_0)^2+(y_0)^2}\\[6pt]\alpha=atan2(y_0,x_0)\\[6pt]x_1=r\times\cos(\alpha+\theta)\\y_1=r\times\sin(\alpha+\theta)',
097| 'rot2': r'\displaystyle{P}_{R}=complex(\cos\theta,\sin\theta)\\[16pt]\\P_0 = complex(x_0, y_0)\\P_1 = P_R \times P_0',
098| 
099| 
100| 'V2C': r"\displaystyle{V}^{2}=\begin{bmatrix}{1}\\{0}\end{bmatrix}=|{0}\rangle",
101| 'V3C': r"\displaystyle{V}^{3}=\begin{bmatrix}{1}\\{0}\\{0}\end{bmatrix}=|{0}\rangle",
102| 'V4C': r"\displaystyle{V}^{4}=\begin{bmatrix}{1}\\{0}\\{0}\\{0}\end{bmatrix}=|{0}\rangle",
103| 
104| 'V2R': r"\displaystyle{V}^{2}=\begin{bmatrix}{1}&{0}\end{bmatrix}=\langle{0}|",
105| 'V3R': r"\displaystyle{V}^{3}=\begin{bmatrix}{1}&{0}&{0}\end{bmatrix}=\langle{0}|",
106| 'V4R': r"\displaystyle{V}^{4}=\begin{bmatrix}{1}&{0}&{0}&{0}\end{bmatrix}=\langle{0}|",
107| 
108| 
109| 'T2':r'T^2 = \displaystyle\begin{bmatrix}\hat{i}_{x}&\hat{j}_{x}\\\hat{i}_{y}&\hat{j}_{y}\end{bmatrix}',
110| 'T3':r'T^3 = \displaystyle\begin{bmatrix}\hat{i}_{x}&\hat{j}_{x}&\hat{k}_{x}\\\hat{i}_{y}&\hat{j}_{y}&\hat{k}_{y}\\\hat{i}_{z}&\hat{j}_{z}&\hat{k}_{z}\end{bmatrix}',
111| 
112| 'I2': r'I^2 = \displaystyle\begin{bmatrix}{1}&{0}\\{0}&{1}\end{bmatrix}',
113| 'I3': r'I^3 = \displaystyle\begin{bmatrix}{1}&{0}&{0}\\{0}&{1}&{0}\\{0}&{0}&{1}\end{bmatrix}',
114| 'I4': r'I^4 = \displaystyle\begin{bmatrix}{1}&{0}&{0}&{0}\\{0}&{1}&{0}&{0}\\{0}&{0}&{1}&{0}\\{0}&{0}&{0}&{1}\end{bmatrix}',
115| 
116| 
117| 'R2xy': r'\displaystyle{R}^{2}_{xy}(\theta)=\begin{bmatrix}{+\cos\theta}&{-\sin\theta}\\{+\sin\theta}&{+\cos\theta}\end{bmatrix}',
118| 
119| 'R2xy0'  :r'\displaystyle{R}^{2}_{xy}({0}^{\circ}) =\begin{bmatrix}{+1}&{0}\\{0}&{+1}\end{bmatrix}',
120| 'R2xy90' :r'\displaystyle{R}^{2}_{xy}({90}^{\circ}) =\begin{bmatrix}{0}&{-1}\\{+1}&{0}\end{bmatrix}',
121| 'R2xy180':r'\displaystyle{R}^{2}_{xy}({180}^{\circ})=\begin{bmatrix}{-1}&{0}\\{0}&{-1}\end{bmatrix}',
122| 'R2xy270':r'\displaystyle{R}^{2}_{xy}({270}^{\circ})=\begin{bmatrix}{0}&{+1}\\{-1}&{0}\end{bmatrix}',
123| 
124| 'R2xy15':r'\displaystyle{R}^{2}_{xy}({15}^{\circ})=\begin{bmatrix}{+0.96592}&{-0.25881}\\{+0.25881}&{+0.96592}\end{bmatrix}',
125| 'R2xy30':r'\displaystyle{R}^{2}_{xy}({30}^{\circ})=\begin{bmatrix}{+0.86602}&{-0.50000}\\{+0.50000}&{+0.86602}\end{bmatrix}',
126| 'R2xy45':r'\displaystyle{R}^{2}_{xy}({45}^{\circ})=\begin{bmatrix}{+0.70711}&{-0.70711}\\{+0.70711}&{+0.70711}\end{bmatrix}',
127| 'R2xy60':r'\displaystyle{R}^{2}_{xy}({60}^{\circ})=\begin{bmatrix}{+0.50000}&{-0.86602}\\{+0.86602}&{+0.50000}\end{bmatrix}',
128| 
129| 'R2xy120':r'\displaystyle{R}^{2}_{xy}({120}^{\circ})=\begin{bmatrix}{-0.50000}&{-0.86602}\\{+0.86602}&{-0.50000}\end{bmatrix}',
130| 'R2xy240':r'\displaystyle{R}^{2}_{xy}({240}^{\circ})=\begin{bmatrix}{-0.50000}&{+0.86602}\\{-0.86602}&{-0.50000}\end{bmatrix}',
131| 
132| 
133| 'R3xy': r'\displaystyle{R}^{3}_{xy}(\theta)=\begin{bmatrix}{+\cos\theta}&{-\sin\theta}&{0}\\{+\sin\theta}&{+\cos\theta}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
134| 
135| 'R3xy0'  : r'\displaystyle{R}^{3}_{xy}(0^{\circ}) =\begin{bmatrix}{+1}&{0}&{0}\\{0}&{+1}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
136| 'R3xy90' : r'\displaystyle{R}^{3}_{xy}(90^{\circ}) =\begin{bmatrix}{0}&{-1}&{0}\\{+1}&{0}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
137| 'R3xy180': r'\displaystyle{R}^{3}_{xy}(180^{\circ})=\begin{bmatrix}{-1}&{0}&{0}\\{0}&{-1}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
138| 'R3xy270': r'\displaystyle{R}^{3}_{xy}(270^{\circ})=\begin{bmatrix}{0}&{+1}&{0}\\{-1}&{0}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
139| 
140| 
141| 'R3xz': r'\displaystyle{R}^{3}_{xz}(\theta)=\begin{bmatrix}{+\cos\theta}&{0}&{-\sin\theta}\\{0}&{+1}&{0}\\{+\sin\theta}&{0}&{+\cos\theta}\end{bmatrix}',
142| 
143| 'R3xz0'  : r'\displaystyle{R}^{3}_{xz}(0^{\circ}) =\begin{bmatrix}{+1}&{0}&{0}\\{0}&{+1}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
144| 'R3xz90' : r'\displaystyle{R}^{3}_{xz}(90^{\circ}) =\begin{bmatrix}{0}&{0}&{-1}\\{0}&{+1}&{0}\\{+1}&{0}&{0}\end{bmatrix}',
145| 'R3xz180': r'\displaystyle{R}^{3}_{xz}(180^{\circ})=\begin{bmatrix}{-1}&{0}&{0}\\{0}&{+1}&{0}\\{0}&{0}&{-1}\end{bmatrix}',
146| 'R3xz270': r'\displaystyle{R}^{3}_{xz}(270^{\circ})=\begin{bmatrix}{0}&{0}&{+1}\\{0}&{+1}&{0}\\{-1}&{0}&{0}\end{bmatrix}',
147| 
148| 
149| 'R3yz': r'\displaystyle{R}^{3}_{yz}(\theta)=\begin{bmatrix}{+1}&{0}&{0}\\{0}&{+\cos\theta}&{-\sin\theta}\\{0}&{+\sin\theta}&{+\cos\theta}\end{bmatrix}',
150| 
151| 'R3yz0'  : r'\displaystyle{R}^{3}_{yz}(0^{\circ}) =\begin{bmatrix}{+1}&{0}&{0}\\{0}&{+1}&{0}\\{0}&{0}&{+1}\end{bmatrix}',
152| 'R3yz90' : r'\displaystyle{R}^{3}_{yz}(90^{\circ}) =\begin{bmatrix}{+1}&{0}&{0}\\{0}&{0}&{-1}\\{0}&{+1}&{0}\end{bmatrix}',
153| 'R3yz180': r'\displaystyle{R}^{3}_{yz}(180^{\circ})=\begin{bmatrix}{+1}&{0}&{0}\\{0}&{-1}&{0}\\{0}&{0}&{-1}\end{bmatrix}',
154| 'R3yz270': r'\displaystyle{R}^{3}_{yz}(270^{\circ})=\begin{bmatrix}{+1}&{0}&{0}\\{0}&{0}&{+1}\\{0}&{-1}&{0}\end{bmatrix}',
155| 
156| 
157| 'R4xy': r'\displaystyle{R}^{4}_{xy}(\theta)=\begin{bmatrix}{+\cos\theta}&{-\sin\theta}&{0}&{0}\\{+\sin\theta}&{+\cos\theta}&{0}&{0}\\{0}&{0}&{+1}&{0}\\{0}&{0}&{0}&{+1}\end{bmatrix}',
158| 'R4xz': r'\displaystyle{R}^{4}_{xz}(\theta)=\begin{bmatrix}{+\cos\theta}&{0}&{-\sin\theta}&{0}\\{0}&{+1}&{0}&{0}\\{+\sin\theta}&{0}&{+\cos\theta}&{0}\\{0}&{0}&{0}&{+1}\end{bmatrix}',
159| 'R4xw': r'\displaystyle{R}^{4}_{xw}(\theta)=\begin{bmatrix}{+\cos\theta}&{0}&{0}&{-\sin\theta}\\{0}&{+1}&{0}&{0}\\{0}&{0}&{+1}&{0}\\{+\sin\theta}&{0}&{0}&{+\cos\theta}\end{bmatrix}',
160| 'R4yz': r'\displaystyle{R}^{4}_{yz}(\theta)=\begin{bmatrix}{+1}&{0}&{0}&{0}\\{0}&{+\cos\theta}&{-\sin\theta}&{0}\\{0}&{+\sin\theta}&{+\cos\theta}&{0}\\{0}&{0}&{0}&{+1}\end{bmatrix}',
161| 'R4yw': r'\displaystyle{R}^{4}_{yw}(\theta)=\begin{bmatrix}{+1}&{0}&{0}&{0}\\{0}&{+\cos\theta}&{0}&{-\sin\theta}\\{0}&{0}&{+1}&{0}\\{0}&{+\sin\theta}&{0}&{+\cos\theta}\end{bmatrix}',
162| 'R4zw': r'\displaystyle{R}^{4}_{zw}(\theta)=\begin{bmatrix}{+1}&{0}&{0}&{0}\\{0}&{+1}&{0}&{0}\\{0}&{0}&{+\cos\theta}&{-\sin\theta}\\{0}&{0}&{+\sin\theta}&{+\cos\theta}\end{bmatrix}',
163| 
164| 'limerick1': r'\displaystyle\frac{{12}+{144}+{20}+({3}\times\sqrt{4})}{7}+({5}\times{11})={9}^{2}+{0}',
165| 'limerick2': r'\displaystyle\int_{1}^{\sqrt[3]{3}}{z}^{2}\;{dz}\times\cos\!\left(\frac{{3}\times\pi}{9}\right)=\ln\sqrt[3]{e}',
166| 
167| }
168| EqNames = list(Eqs.keys())
169| 
170| EqGet = lambda name: quote_plus(Eqs[name])
171| 
172| Notes = {
173| 'limerick1': 'A dozen, a gross, and a score; Plus three times the square root of four; Divided by seven; Plus five times eleven; Equals nine squared and not a bit more.',
174| 'limerick2': 'Integral z-squared dz; From 1 to the cube root of 3; Times the cosine; Of three pi over 9; Equals log of the cube root of e.',
175| }
176| 
177| ##================================================================================================##
178| def latex_unquote (url):
179|     """Convert an encoded URL into a plain string."""
180|     s = unquote_plus(url)
181|     Log.info()
182|     Log.info(s)
183|     Log.info()
184|     return s
185| 
186| ##================================================================================================##
187| def latex_maker (url, fname='latex.png', fpath=HtmlPath):
188|     """Get a LaTeX image from a server."""
189|     Log.info(url)
190|     Log.info()
191|     # Get the LaTeX image from server...
192|     latex = htget(url)
193|     Log.info(unquote(url)) #ATTN: unquote_plus?
194|     Log.info(latex.status)
195|     #for hdr in latex.headers:
196|     # Log.info(hdr)
197|     Log.info()
198|     Log.info('bytes: %d' % len(latex.data))
199|     Log.info()
200|     if latex.statusCode == 200:
201|         filename = path.join(fpath, fname)
202|         latex.save(filename)
203|         Log.info('wrote: %s' % filename)
204|         Log.info()
205| 
206| ##================================================================================================##
207| def latex_maker_1 (eq_name, size=75, fname='latex_%s.png'):
208|     """Generate a URL for a LaTeX image. (Google)"""
209|     tex = EqGet(eq_name)
210|     url = UrlGoogle % (size, tex)
211|     latex_maker(url, fname=(fname % eq_name.lower()))
212|     return (tex,url)
213| 
214| ##================================================================================================##
215| def old_latex_google (eq_name, size=50, fpath=BasePath):
216|     """Given an Equation Name, get a LaTeX images. (OBS)"""
217|     tex = EqGet(eq_name)
218|     url = UrlGoogle % (size, tex)
219|     # Get the LaTeX image from server...
220|     latex = htget(url)
221|     Log.info(unquote(url))
222|     Log.info(latex.status)
223|     #for hdr in latex.headers:
224|     # Log.info(hdr)
225|     Log.info()
226|     Log.info('bytes: %d' % len(latex.data))
227|     Log.info()
228|     if latex.statusCode == 200:
229|         filename = path.join(fpath, 'latex_%s.png' % eq_name)
230|         latex.save(filename)
231|         Log.info('wrote: %s' % filename)
232|         Log.info()
233| 
234| ##================================================================================================##
235| def latex_maker_2 (eq_name, size=0, fg='000000', bg='ffffff', fname='latex_%s.png'):
236|     """Generate a URL for a LaTeX image. (WordPress)"""
237|     tex = EqGet(eq_name)
238|     url = UrlWordPress % (tex, size, fg, bg)
239|     latex_maker(url, fname=(fname % eq_name.lower()))
240|     return (tex,url)
241| 
242| ##================================================================================================##
243| def latex_maker_3 (eq_name):
244|     """Generate a URL for a LaTeX image. (HostMath)"""
245|     tex = quote_plus(Eqs[eq_name])
246|     url = r'https://www.HostMath.com/Show.aspx?Code=%s'
247|     return url % tex
248| 
249| ##================================================================================================##
250| def make_webpage (fname='index.html', fpath=HtmlPath):
251|     """Create a webpage listing all equations."""
252|     css_table = 'font-style:sans-serif;font-size:10pt;border-spacing:5px;padding:0;border:1px solid black;margin:12pt;'
253|     css_index = 'text-align:right;vertical-align:top;font-weight:bold;padding:2px;'
254|     css_image = 'background-color:#ffffff;padding:2px;border:1px solid black;'
255|     css_caption = 'background-color:#bfbfbf;font-weight:bold;padding:3px;margin-top:10px;'
256|     css_latex = 'display:none;font-family:monospace;font-size:9pt;padding-top:4px;'
257|     js = 'function toggle(n){var s="eq_"+n.substring(3);var div=document.getElementById(s);div.style.display=div.style.display=="none"?"block":"none";}'
258|     # Create new webpage object...
259|     pg = WebPage('LaTeX Equations', h2='The Library', jstxt=js)
260|     # Add content...
261|     pg.add('<table style="%s">' % css_table)
262|     pg.add('<tbody>')
263| 
264|     for ix,name in enumerate(EqNames, start=1):
265|         pg.add('<tr>')
266|         pg.add(f'<td style="{css_index}">{ix}</td>')
267|         pg.add(f'<td style="{css_image}">')
268|         pg.add(f'<img id="im_{name}" src="latex_{name.lower()}.png" onclick="toggle(this.id);">')
269|         pg.add(f'<div id="eq_{name}" style="{css_latex}">{Eqs[name]}</div>')
270|         pg.add(f'<div style="{css_caption}">{name if name else "\"\""}</div>')
271|         pg.add('</td>')
272|         pg.add('</tr>')
273| 
274|     pg.add('</tbody>')
275|     pg.add('</table>')
276|     pg.add()
277|     # Save the webpage...
278|     fn = path.join(fpath, fname)
279|     pg.save(fn)
280|     Log.info(f'wrote: {fn}')
281|     Log.info()
282| 
283| 
284| ##================================================================================================##
285| def do_latex_maker_1 (**kwargs):
286|     """Get a LaTeX equation image. (Google)"""
287|     fpath = KWARG('fpath', HtmlPath, str, kwargs)
288|     fname = KWARG('fname', 'latex_%s.png', str, kwargs)
289|     eqname = KWARG('eq', '_', str, kwargs)
290|     eqsize = KWARG('sz', 50, int, kwargs)
291|     Log.info('latex-maker-1:: %s' % str(list(kwargs.keys())))
292|     Log.debug('fpath="%s"' % fpath)
293|     Log.debug('fname="%s"' % fname)
294|     Log.debug('eq-name="%s"' % eqname)
295|     Log.debug('eq-size=%d' % eqsize)
296|     Log.info()
297|     url = latex_maker_1(eqname, size=eqsize, fname=fname)
298|     Log.info(url[0])
299|     Log.info()
300|     Log.info(url[1])
301|     Log.info()
302|     return 'Done'
303| ##================================================================================================##
304| def do_latex_maker_2 (**kwargs):
305|     """Get a LaTeX equation image. (WordPress)"""
306|     fpath = KWARG('fpath', HtmlPath, str, kwargs)
307|     fname = KWARG('fname', 'latex_%s.png', str, kwargs)
308|     eqname = KWARG('eq', '_', str, kwargs)
309|     eqsize = KWARG('sz', 0, int, kwargs)
310|     fgcolor = KWARG('fg', '000000', str, kwargs)
311|     bgcolor = KWARG('bg', 'ffffff', str, kwargs)
312|     Log.info('latex-maker-2:: %s' % str(list(kwargs.keys())))
313|     Log.debug('fpath="%s"' % fpath)
314|     Log.debug('fname="%s"' % fname)
315|     Log.debug('eq-name="%s"' % eqname)
316|     Log.debug('eq-size=%d' % eqsize)
317|     Log.debug('fg/bg=%s, %s' % (fgcolor,bgcolor))
318|     Log.info()
319|     url = latex_maker_2(eqname, size=eqsize, fg=fgcolor, bg=bgcolor, fname=fname)
320|     Log.info(url[0])
321|     Log.info()
322|     Log.info(url[1])
323|     Log.info()
324|     return 'Done'
325| ##================================================================================================##
326| def do_get_all_1 (**kwargs):
327|     """Get all LaTeX equation images! (Google)"""
328|     fpath = KWARG('fpath', HtmlPath, str, kwargs)
329|     fname = KWARG('fname', 'latex_%s.png', str, kwargs)
330|     eqpatt = KWARG('eq', '', str, kwargs)
331|     eqsize = KWARG('sz', 50, int, kwargs)
332|     Log.info('latex-get-all-1:: %s' % str(list(kwargs.keys())))
333|     Log.debug('fpath="%s"' % fpath)
334|     Log.debug('fname="%s"' % fname)
335|     Log.debug('eq-patt="%s"' % eqpatt)
336|     Log.debug('eq-size=%d' % eqsize)
337|     Log.info()
338|     for n in EqNames:
339|         if len(eqpatt) and not n.startswith(eqpatt):
340|             continue
341|         url = latex_maker_1(n, size=eqsize, fname=fname)
342|     Log.info()
343|     return 'Done'
344| ##================================================================================================##
345| def do_get_all_2 (**kwargs):
346|     """Get all LaTeX equation images! (WordPress)"""
347|     fpath = KWARG('fpath', HtmlPath, str, kwargs)
348|     fname = KWARG('fname', 'latex_%s.png', str, kwargs)
349|     eqpatt = KWARG('eq', '', str, kwargs)
350|     eqsize = KWARG('sz', 0, int, kwargs)
351|     fgcolor = KWARG('fg', '000000', str, kwargs)
352|     bgcolor = KWARG('bg', 'fcfff8', str, kwargs)
353|     Log.info('latex-get-all-2:: %s' % str(list(kwargs.keys())))
354|     Log.debug('fpath="%s"' % fpath)
355|     Log.debug('fname="%s"' % fname)
356|     Log.debug('eq-patt="%s"' % eqpatt)
357|     Log.debug('eq-size=%d' % eqsize)
358|     Log.debug('fg/bg=%s, %s' % (fgcolor,bgcolor))
359|     Log.info()
360|     for n in EqNames:
361|         if len(eqpatt) and not n.startswith(eqpatt):
362|             continue
363|         url = latex_maker_2(n, size=eqsize, fg=fgcolor, bg=bgcolor, fname=fname)
364|     Log.info()
365|     return 'Done'
366| ##================================================================================================##
367| def do_make_webpage (**kwargs):
368|     """Generate the LaTeX Equations Webpage."""
369|     fpath = KWARG('fpath', HtmlPath, str, kwargs)
370|     fname = KWARG('fname', 'index.html', str, kwargs)
371|     Log.info('make-webpage:: %s' % str(list(kwargs.keys())))
372|     Log.debug('fpath="%s"' % fpath)
373|     Log.debug('fname="%s"' % fname)
374|     Log.info()
375| 
376|     make_webpage()
377| 
378|     Log.info()
379|     return 'Done'
380| ##================================================================================================##
381| def do_test (**kwargs):
382|     """Do a test."""
383|     fpath = KWARG('fpath', BasePath, str, kwargs)
384|     fname = KWARG('fname', 'test.out', str, kwargs)
385|     eqname = KWARG('eq', '_', str, kwargs)
386|     eqsize = KWARG('sz', 50, int, kwargs)
387|     Log.info(f'test:: {list(kwargs.keys())}')
388|     Log.debug(f'{fpath=}')
389|     Log.debug(f'{fname=}')
390|     Log.debug(f'eq-name="{eqname}"')
391|     Log.debug(f'eq-size={eqsize}')
392|     Log.info()
393| 
394|     old_latex_google(eqname, size=eqsize)
395| 
396|     Log.info()
397|     return 'Done'
398| ####################################################################################################
399| def dispatch (cmd, **kwargs):
400|     """Dispatch to a module function based on CMD."""
401|     Log.info(f'CMD: {cmd}')
402|     for kw in kwargs:
403|         Log.debug(f'argument[{kw}]: {kwargs[kw]}')
404|     Log.info()
405|     if cmd == '/1': return do_latex_maker_1(**kwargs)
406|     if cmd == '/2': return do_latex_maker_2(**kwargs)
407|     if cmd == '@1': return do_get_all_1(**kwargs)
408|     if cmd == '@2': return do_get_all_2(**kwargs)
409|     if cmd == 'html': return do_make_webpage(**kwargs)
410|     if cmd == 'test': return do_test(**kwargs)
411|     return 'Nothing to do!'
412| ####################################################################################################
413| if __name__ == '__main__':
414|     print(f'autorun: {argv[0]}')
415|     Log.start(path.join(BasePath,'latex.log'))
416|     Log.level(info())
417|     Log.info(argv[0])
418|     Log.info()
419|     cmd = argv[1] if 1 < len(argv) else CMND
420|     kwargs = ARGS
421|     for a in argv[2:]:
422|         nv = a.split('=', 1)
423|         kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
424|         Log.debug(f'{nv[0]}: "{kwargs[nv[0]]}"')
425|     try:
426|         print(f'CMD: {cmd}')
427|         print()
428|         obj = dispatch(cmd, **kwargs)
429|         print()
430|         print(obj)
431|         Log.info()
432|         Log.info(obj)
433|     except:
434|         etype, evalue, tb = exc_info()
435|         ts = extract_tb(tb)
436|         Log.error()
437|         Log.error(f'{etype.__name__}: {evalue!s}')
438|         Log.error()
439|         for t in ts[-5:]:
440|             Log.error(f'[{t[1]}] {t[2]} ({t[0]})')
441|             Log.error(f' {t[3]}')
442|             Log.error()
443|         raise
444|     finally:
445|         Log.end()
446| ####################################################################################################
447| '''eof'''
448|