Skip to content

Commit

Permalink
[24_15] Add docx converter & test (#1985)
Browse files Browse the repository at this point in the history
<!-- Thank you for your contribution! -->
## What
Added converter for tm, tmu to docx.

## How to test your changes?
`xmake r 24_15`
  • Loading branch information
ATQlove authored Aug 4, 2024
1 parent 8735bd7 commit 302b78e
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci-xmake-archlinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ jobs:
sudo pacman -Syyu --noconfirm --needed
sudo pacman -S --noconfirm --needed qt6-base qt6-svg libpng zlib \
libjpeg curl python libxext xmake unzip noto-fonts-cjk \
fontconfig libgit2 freetype2 openssl git mimalloc cmake
fontconfig libgit2 freetype2 openssl git mimalloc cmake \
pandoc
- uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-xmake-debian-bookworm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
DEBIAN_FRONTEND=noninteractive apt-get install -y gcc git 7zip unzip curl build-essential \
fonts-noto-cjk libcurl4-openssl-dev libfreetype-dev libfontconfig-dev \
libmimalloc-dev libgit2-dev zlib1g-dev libssl-dev libjpeg62-turbo-dev cmake \
qt6-base-dev libqt6svg6-dev qt6-image-formats-plugins
qt6-base-dev libqt6svg6-dev qt6-image-formats-plugins pandoc
- uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci-xmake-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ jobs:
version: 6.5.3
target: 'desktop'
cache: 'true'

- name: Install pandoc
run: brew install pandoc

- name: set XMAKE_GLOBALDIR
run: echo "XMAKE_GLOBALDIR=${{ runner.workspace }}/xmake-global" >> $GITHUB_ENV
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-xmake-ubuntu-jammy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ jobs:
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y gcc \
fonts-noto-cjk libcurl4-openssl-dev libfreetype-dev \
${{matrix.qt_pkg}} libgit2-dev zlib1g-dev libssl-dev cmake
${{matrix.qt_pkg}} libgit2-dev zlib1g-dev libssl-dev cmake \
pandoc
- uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-xmake-ubuntu-noble.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y gcc \
fonts-noto-cjk libcurl4-openssl-dev libfreetype-dev \
${{matrix.qt_pkg}} libgit2-dev zlib1g-dev libssl-dev cmake \
libmimalloc-dev xmake libjpeg-dev libfontconfig-dev
libmimalloc-dev xmake libjpeg-dev libfontconfig-dev pandoc
- uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci-xmake-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ jobs:
- name: update repo
run: |
xrepo update-repo
- name: Install pandoc
run: choco install pandoc -y

- uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
66 changes: 66 additions & 0 deletions TeXmacs/plugins/docx/progs/data/docx.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MODULE : docx.scm
;; DESCRIPTION : DOCX data format
;; COPYRIGHT : (C) 2024 ATQlove
;;
;; This software falls under the GNU general public license version 3 or later.
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(texmacs-module (data docx)
(:use (binary pandoc)
(texmacs texmacs tm-files)
(network url)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to export TeXmacs document to DOCX using Pandoc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(tm-define (export-to-docx input-file output-file)
(:synopsis "Export TeXmacs document to DOCX format using Pandoc")
(let* ((base-dir (url-head input-file))
(html-file-url (url-glue (url-head output-file) ".html")))
;; First, export the document to HTML
(tm->html input-file html-file-url)
;; Then, use Pandoc to convert the HTML to DOCX
(if (has-binary-pandoc?)
(let ((cmd (string-append (url->string (find-binary-pandoc))
" -f html -t docx "
(url->string html-file-url)
" -o "
(url->string output-file))))
(system cmd)
;; Delete the intermediate HTML file
(system-remove html-file-url)) ;; Expected:$TEXMACS_PATH/tests/tm.html")
(error "Pandoc binary not found"))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper function to export tm/tmu to HTML
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(tm-define (tm->html tm-file-url html-file-url)
; Step 1: load the tm file
(display* "load-buffer: " tm-file-url "\n")
(load-buffer tm-file-url)
; it will crash when loading the buffer in beamer style before this pull request
(display* "buffer-loaded: " tm-file-url "\n")

; Step 2: cleaning for the html file url
(when (url-exists? html-file-url) (system-remove html-file-url))

; Step 3: Export the buffer to the html file url
(display* "export buffer to: " html-file-url)
(export-buffer-main (current-buffer) html-file-url "html" ())
#t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Converter for exporting TeXmacs files to DOCX
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(converter texmacs-file docx-file
(:require (has-binary-pandoc?))
(:function-with-options export-to-docx))
40 changes: 40 additions & 0 deletions TeXmacs/tests/24_15.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MODULE : 24_15.scm
;; DESCRIPTION : Test for docx.scm
;; COPYRIGHT : (C) 2024 ATQlove
;;
;; This software falls under the GNU general public license version 3 or later.
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(import (srfi srfi-78))
(use-modules (data docx))

(define (tm->docx tm-file-url docx-file-url)
; Step 1: load the tm file
; (display* "load-buffer: " tm-file-url "\n")
(load-buffer tm-file-url)
; (display* "buffer-loaded: " tm-file-url "\n")

; Step 2: Export the buffer to the docx file url
; (display* "export buffer to: " docx-file-url "\n")
(export-to-docx tm-file-url docx-file-url)

; Check if the docx file exists
(if (url-exists? docx-file-url)
(display* "Export successful: " docx-file-url "\n")
(display* "Export failed: " docx-file-url "\n"))

(url-exists? docx-file-url))

(define (test_24_15)
(display (url-exists? (system->url "$TEXMACS_PATH/tests/tm/24_15.tm")))
(let* ((tm-url "$TEXMACS_PATH/tests/tm/24_15.tm")
(docx-url "$TEXMACS_PATH/tests/tm/24_15.docx")
(result (tm->docx tm-url docx-url)))
(check result => #t)
(check-report)
(if (check-failed?) (exit -1))))
81 changes: 81 additions & 0 deletions TeXmacs/tests/tm/24_15.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<TeXmacs|2.1.2>

<style|<tuple|beamer|chinese|doc>>

<\body>
<screens|<\shown>
<tit|\<#7B2C\>0\<#8BB2\>\<#FF1A\>\<#521D\>\<#8BC6\>\<#6570\>\<#5B66\>\<#6A21\>\<#5F0F\>>

<\overlays|2|2>
<\big-table|<tabular|<tformat|<cwith|1|1|1|-1|cell-halign|c>|<cwith|2|-1|2|2|cell-halign|r>|<table|<row|<cell|\<#83DC\>\<#5355\>>|<cell|\<#5FEB\>\<#6377\>\<#952E\>>>|<row|<cell|<menu|Insert|Math|Inline
formula>>|<cell|<key|$>>>|<row|<cell|<menu|Insert|Math|Displayed
formula>>|<cell|<key|A-$>>>|<row|<cell|<menu|Insert|Math|Several
equations>>|<cell|<key|A-&>>>>>>>
\<#8FDB\>\<#5165\>\<#6570\>\<#5B66\>\<#6A21\>\<#5F0F\>
</big-table>

\<#8FD9\>\<#662F\>\<#4E00\>\<#4E2A\>\<#6BB5\>\<#843D\>\<#FF0C\>\<#5B83\>\<#53EF\>\<#80FD\>\<#662F\>\<#4E00\>\<#884C\>\<#4E5F\>\<#53EF\>\<#80FD\>\<#662F\>\<#591A\>\<#884C\>\<#3002\>\<#8FD9\>\<#662F\>\<#4E00\>\<#4E2A\>\<#884C\>\<#5185\>\<#516C\>\<#5F0F\><math|\<alpha\>+\<beta\>>\<#FF0C\>\<#5B83\>\<#53EF\>\<#4EE5\>\<#88AB\>\<#5D4C\>\<#5165\>\<#5728\>\<#6BB5\>\<#843D\>\<#4E2D\>\<#3002\>

<\overlay-this|2>
<\big-table|<tabular|<tformat|<cwith|2|2|3|3|cell-row-span|1>|<cwith|2|2|3|3|cell-col-span|2>|<cwith|1|-1|3|4|cell-halign|c>|<table|<row|<cell|\<#7ED3\>\<#6784\>\<#53D8\>\<#5143\>\<#7684\>\<#8F6E\>\<#6362\>>|<cell|\<#884C\>\<#5185\>\<rightarrow\>\<#5355\>\<#884C\>>|<cell|<key|A-S-up>>|<cell|<key|A-S-down>>>|<row|<cell|\<#5207\>\<#6362\>>|<cell|\<#5355\>\<#884C\><math|\<rightarrow\>>\<#591A\>\<#884C\>>|<cell|<key|C-&>>|<cell|>>>>>>
\<#8F6E\>\<#6362\>\<#548C\>\<#5207\>\<#6362\>
</big-table>

<equation*|<around*|(|x+1|)><rsup|3>=<around*|(|x<rsup|2>+2*x+1|)>*<around*|(|x+1|)>=x<rsup|3>+3*x<rsup|2>+3*x+1>
</overlay-this|>
</overlays>

\<#672C\>\<#9875\>\<#5E7B\>\<#706F\>\<#7247\>\<#FF1A\>\<#70B9\>\<#51FB\><strong|<menu|Help|Planet>>\<#FF0C\>\<#70B9\>\<#51FB\><strong|\<#4ECE\>\<#96F6\>\<#5F00\>\<#59CB\>\<#5B66\>\<#58A8\>\<#5E72\>\<#7406\>\<#5DE5\>\<#5957\>\<#4EF6\>>\<#5373\>\<#53EF\>\<#627E\>\<#5230\>

\;

\;

\;
</shown>>
</body>

<\initial>
<\collection>
<associate|info-flag|minimal>
<associate|page-height|auto>
<associate|page-medium|beamer>
<associate|page-screen-margin|false>
<associate|page-type|16:9>
<associate|page-width|auto>
</collection>
</initial>

<\references>
<\collection>
<associate|auto-1|<tuple|1|1>>
<associate|auto-2|<tuple|1|1>>
<associate|auto-3|<tuple|1|1>>
<associate|auto-4|<tuple|1|1>>
<associate|auto-5|<tuple|2|2>>
<associate|auto-6|<tuple|2|2>>
</collection>
</references>

<\auxiliary>
<\collection>
<\associate|idx>
<tuple|<tuple|<with|font-family|<quote|ss>|\<#63D2\>\<#5165\>>|<with|font-family|<quote|ss>|\<#6570\>\<#5B66\>>|<with|font-family|<quote|ss>|\<#884C\>\<#5185\>\<#516C\>\<#5F0F\>>>|<pageref|auto-2>>

<tuple|<tuple|<with|font-family|<quote|ss>|\<#63D2\>\<#5165\>>|<with|font-family|<quote|ss>|\<#6570\>\<#5B66\>>|<with|font-family|<quote|ss>|\<#5355\>\<#884C\>\<#516C\>\<#5F0F\>>>|<pageref|auto-3>>

<tuple|<tuple|<with|font-family|<quote|ss>|\<#63D2\>\<#5165\>>|<with|font-family|<quote|ss>|\<#6570\>\<#5B66\>>|<with|font-family|<quote|ss>|\<#591A\>\<#884C\>\<#516C\>\<#5F0F\>>>|<pageref|auto-4>>

<tuple|<tuple|<with|font-family|<quote|ss>|\<#5E2E\>\<#52A9\>>|<with|font-family|<quote|ss>|\<#58A8\>\<#5BA2\>\<#661F\>\<#7403\>>>|<pageref|auto-6>>
</associate>
<\associate|table>
<tuple|normal|<\surround|<hidden-binding|<tuple>|1>|>
\<#8FDB\>\<#5165\>\<#6570\>\<#5B66\>\<#6A21\>\<#5F0F\>
</surround>|<pageref|auto-1>>

<tuple|normal|<\surround|<hidden-binding|<tuple>|2>|>
\<#8F6E\>\<#6362\>\<#548C\>\<#5207\>\<#6362\>
</surround>|<pageref|auto-5>>
</associate>
</collection>
</auxiliary>

0 comments on commit 302b78e

Please sign in to comment.