【背景】node
下面的文本(https://www.aimsciences.org/article/doi/10.3934/cpaa.2009.8.1725)python
Global well-posedness for the $L^2$-critical Hartree equation on $\mathbb{R}^n$, $n\ge 3$
被 MathJax 渲染成npm
須要解析成bash
Global well-posedness for the L2-critical Hartree equation on Rn, n≥3
【環境】ide
OS 版本:Windows10 x64 1803函數
Python版本:Python 3.6.5 x64ui
Node.js 版本:Node.js 10.14.2code
mathjax-node:mathjax-node@2.1.1orm
npm install -g mathjax-node npm list --depth=0 -global set node_path=C:\Users\walker\AppData\Roaming\npm\node_modules
【t.js】xml
var mjAPI = require("mathjax-node") function MathJax2Xml(mathjaxFormula) { mjAPI.config({ }); mjAPI.start(); mjAPI.typeset({ math: mathjaxFormula, format: "TeX", mml: true }, function (data) { if (!data.errors) { console.log(data.mml); } else { console.log("<p>ERROR</p>"); } }); } var args = process.argv.splice(2); MathJax2Xml(args[0])
【t.py】
#encoding: utf-8 #author: walker # date: 2019-05-17 # summary: 調用 nodejs 處理 mathjax 公式 import re from subprocess import check_output from parsel import Selector def GetXml(mathjaxFormula): r""" 將 mathjax 公式轉爲 xml """ bytesTxt = check_output(['node', 't.js', mathjaxFormula], timeout=100) xmlText = bytesTxt.decode('utf8').strip() # print('xmlText: %s' % xmlText) return xmlText def Xml2PlainText(xmlText): r""" 將 xml 轉換爲普通文本 """ sel = Selector(text=xmlText, type='xml') plainText = sel.xpath('string(.)').get().strip() plainText = re.sub(r'\s+', '', plainText) # 去掉空白 # print('plainText: %s' % plainText) return plainText def FnRepl(matched): r""" re.sub 的回調函數 """ mathjaxFormula = matched.group(0) mathjaxFormula = mathjaxFormula[1:-1] # 去掉先後的 $ 符號 return Xml2PlainText(GetXml(mathjaxFormula)) def Convert(mathjaxText): plainText = re.sub('\$[\s\S]+?\$', FnRepl, mathjaxText) plainText = re.sub(r'\s+', ' ', plainText) # 將多餘空白替換成單個空格 return plainText if __name__ == '__main__': mathjaxText = 'Global well-posedness for the $L^2$-critical Hartree equation on $\mathbb{R}^n$, $n\ge 3$' plainText = Convert(mathjaxText) print('mathjaxText: %s' % mathjaxText) print('plainText: %s' % plainText)
【相關閱讀】
*** walker ***