TeXmacs開發:用tm2md將TeXmacs文檔轉換爲markdown文檔

tm2md:簡單有效的TeXmacs到markdown的轉換器

代碼在GitHub: https://github.com/PikachuHy/texmacs-converter-tm2md
(注:爲了行文方便,本文采用中文編寫)

tm2md原先的代碼在
bitbucket,這裏是經過我魔改後的代碼。

tm2md是什麼

tm2md是爲TeXmacs開發的一個轉換器,可以方便的將TeXmacs文檔轉換爲markdown文檔,目前支持大部分的markdown語法。

在原代碼倉庫的README中是這樣寫的:

It supports all (most?) of the standard Markdown syntax, plus much of TeXmacs’
non-dynamic markup. In particular, labels and references, numbered environments
and figures should work out of the box. The major exception are tables, but
this can be overcome by exporting them to html and pasting into the markdown
file.

我的主要貢獻有:

  • 保持轉換的md文檔可以在Typora上正常渲染,儘量保持兼容CSDN博客md編輯器
  • 重寫行內公式、行間公式和多行公式的轉換代碼
  • 修復導出的含\ensuremath的公式無法在Typora渲染的問題
  • 修復導出的標題編碼錯誤問題
  • 初步支持導出TeXmacs Session

安裝和使用

安裝

下載本項目到本地,放在convert目錄下,重命名爲markdown。在windows上,路徑爲%appdata%/TeXmacs/progs/convert,在Linux或MacOS上,路徑爲~/.TeXmacs/progs/convert

例如,在MacOS上通過git安裝

mkdir -p ~/.TeXmacs/progs/convert
cd ~/.TeXmacs/progs/convert
git clone https://github.com/PikachuHy/texmacs-converter-tm2md.git markdown

然後在my-init-texmacs.scm添加代碼

(use-modules (convert markdown init-markdown))

注:

在TeXmacs中快速打開my-init-texmacs.scm文件的方法是點擊菜單Developer->Open my-init-texmacs.scm

如果沒有Developer菜單,可以先點擊菜單Tools->Developer tool打開Developer菜單。

使用

成功安裝後,可以看到File->Export->Markdown…菜單,點擊即可導出爲markdown文檔。

爲什麼要開發tm2md

TeXmacs是我目前遇到的最方便的數學公式編輯器,沒有之一。我平時使用Typora編輯器寫markdown,但是它的數學公式編輯功能比較弱。另外,我平時還會把寫的一些東西發到CSDN博客。所以我需要一個工具,可以把TeXmacs文檔轉換成markdown文檔。找啊找,找到了tm2md,但這個東西有些轉換效果不好,最直接的,行內公式和多行公式在Typora就不能正常渲染。

我對tm2md的需求:

  • 導出的數學公式一定要渲染正常,渲染準確
  • 保持對Typora的兼容性
  • 導出的markdown文檔,在CSDN博客編輯器編輯時,儘量少的改動

最後我在原代碼的基礎上做了些改動,變成現在這個樣子。

參與開發

轉換流程

TeXmacs doc =>TeXmacs stree => markdown stree => markdown doc

  • TeXmacs doc =>TeXmacs stree 是內部自動的
  • TeXmacs stree => markdown stree 是調用texmacs->markdown
  • markdown stree=>markdown doc 是調用 serialize-markdown

還有一個函數是serialize-markdown-document,它對轉換的markdown文檔做一些調整。

serialize-markdown-document會調用serialize-markdown

如何調試

  • 首先要拿到TeXmacs stree
    準備好測試文檔,通過點擊菜單File->Export->TeXmacs Scheme…,保存到文件的內容就是我們需要的TeXmacs stree。
  • 搭建好測試的函數環境
    通過Insert->Session->Scheme得到Guile的執行環境
    通過執行texmacs->markdown拿到markdown stree
    通過執行serialize-markdown得到轉換後的markdown文檔內容

例如

;;; Scheme] 
(define md_st '(document (TeXmacs "1.99.12") (style (tuple "generic" "chinese")) (body (document "<#6570><#5B66><#516C><#5F0F><#6D4B><#8BD5>" (equation* (document (concat "a" (rsup "2") "+b" (rsup "2") "=c" (rsup "2")))))) (initial (collection (associate "preamble" "false")))))

;;; Scheme] 
(texmacs->markdown md_st)

(document “<#6570><#5B66><#516C><#5F0F><#6D4B><#8BD5>” ("a2+b2=c2 a^2 + b^2 = c^2 "))

;;; Scheme] 
(serialize-markdown-document (texmacs->markdown md_st))

"æŢřåŋęå\x85ňåij\x8fæţ\x8bèŕŢ

a2+b2=c2 a^2 + b^2 = c^2 "

數學公式轉換

主要是調用TeXmacs轉LaTex代碼

(define (math->latex t)
 "Converts the TeXmacs tree @t into internal LaTeX representation"
 (with options '(("texmacs->latex:replace-style" . "on")
                 ("texmacs->latex:expand-macros" . "on")
                 ("texmacs->latex:expand-user-macros" . "off")
                 ("texmacs->latex:indirect-bib" . "off")
                 ("texmacs->latex:encoding" . "utf8")
                 ("texmacs->latex:use-macros" . "off"))
 (texmacs->latex t options)))

已知問題

  • 不能導出圖片
  • 不能導出表格
  • 導出超鏈接時,前面有一個沒必要的換行符
  • 行內公式中英文混排,如果行內公式前面有中文,行內公式內的中文會亂碼
  • 公式導出時出現一行公式的內容出現在兩行中

未來開發計劃

  • 支持導出圖片

License

GPLv3

TeXmacs相關

  • TeXmacs中文社區QQ羣:934456971,羣主(不是我)在週日不定期直播TeXmacs的使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章