python调用node_Python3 调用 Node.js 解

【背景】Global well-posedness for the $L^2$-critical Hartree  equation on $\mathbb{R}^n$, $n\ge 3$

被 MathJax 渲染成

需要解析成Global well-posedness for the L2-critical Hartree equation on Rn, n≥3

【环境】OS 版本:Windows10 x64 1803

Python版本:Python 3.6.5 x64

Node.js 版本:Node.js 10.14.2

mathjax-node:mathjax-node@2.1.1npm install -g mathjax-node

npm list --depth=0 -global

set node_path=C:\Users\walker\AppData\Roaming\npm\node_modules

【t.js】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("

ERROR

");

}

});

}

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)

【相关阅读】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值