利用git+pandoc完成對word文檔的版本控制


搞這個事情的初衷是因為嘗到了git甜頭想要搞點事情而已。

因為.doc是個二進制文件,git只能顯示純文本文檔的修改變化,所以帶上pandoc作一下格式轉換。

參考資料:
https://blog.csdn.net/GAI159/article/details/105025312/
https://www.cnblogs.com/yezuhui/p/6853271.html
https://github.com/vigente/gerardus/wiki/Integrate-git-diffs-with-word-docx-files
Pandoc的安裝與使用

Pandoc簡介

本部分內容參考
Pandoc的安裝與使用
作者:grug350 來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處

官網:Pandoc

Pandoc是個好用的文本格式轉化器。它可以將文檔在 Markdown、LaTeX、reStructuredText、HTML、Word docx 等多種標記格式之間相互轉換,並支持輸出 PDF、EPUB、HTML 幻燈片等多種格式。該程序被稱為格式轉換界的 “瑞士軍刀”。這里用來把word轉成markdown,好讓我們能在git的管理下看見詳細的版本控制信息。

1. 安裝

安裝參考官網就好,支持Windows,macOS,Linux。Linux蠻多發行版自帶這個,比如Ubuntu,或則說如果安裝過Anacoda,那么pandoc也會同時裝入電腦中。

我的電腦里裝了Anacoda,所以直接拿來用就好了。

2.pandoc基本使用

pandoc [OPTIONS] [FILES]

#基本參數說明
其中 <files> 為輸入的內容,其輸入即可以來自文件,也可以來自標准輸入甚至網頁鏈接。而 <options> 為參數選項。主要的參數選項有:

-f <format>、-r <format> # 指定輸入文件格式,默認為 Markdown;
-t <format>、-w <format> # 指定輸出文件格式,默認為 HTML;
注意:上述兩項pandoc可以根據文件拓展名自己識別,不輸入也行
-o <file> # 指定輸出文件,該項缺省時,將輸出到標准輸出;
--highlight-style <style> # 設置代碼高亮主題,默認為 pygments;
-s # 生成有頭尾的獨立文件(HTML,LaTeX,TEI 或 RTF);
-S # 聰明模式,根據文件判斷其格式;
--self-contained # 生成自包含的文件,僅在輸出 HTML 文檔時有效;
--verbose # 開啟 Verbose 模式,用於 Debug;
--list-input-formats # 列出支持的輸入格式;
 # 列出支持的輸出格式;
--list-extensions # 列出支持的 Markdown 擴展方案;
--list-highlight-languages # 列出支持代碼高亮的編程語言;
--list-highlight-styles # 列出支持的代碼高亮主題;
-v、--version # 顯示程序的版本號;
-h、--help # 顯示程序的幫助信息。

#使用示范
pandoc input.docx -o output.md #把我的word轉成markdown
pandoc input.md -o output.docx

實現對word文檔的版本控制

參考教程地址:https://github.com/vigente/gerardus/wiki/Integrate-git-diffs-with-word-docx-files

This section was inspired by Martin Fenner's "Using Microsoft Word with git".

To configure git diff:

Install pandoc.

Tell git how to handle diffs of .docx files.

Create or edit file ~/.gitconfig (linux, Mac) or "c:\Documents and Settings\user.gitconfig" (Windows) to add

 [diff "pandoc"]
   textconv=pandoc --to=markdown
   prompt = false
 [alias]
   wdiff = diff --word-diff=color --unified=1

In your paper directory, create or edit file .gitattributes (linux, Windows and Mac) to add

*.docx diff=pandoc

You can commit .gitattributes so that it stays with your paper for use in other computers, but you'll need to edit ~/.gitconfig in every new computer you want to use.

========================================================================================================================================

.gitattributes

稍微說明一下.gitatrributes這個文件。官網鏈接:gitattributes documents
這個文件是一個簡單的文本文件,作用是為attributes提供一個路徑名

文件的基本格式

pattern attr1 attr2 ...

是一個pattern跟隨一串的attrs, 這個pattern我理解的是某種匹配模式,然后會找到符合這種匹配模式的文件,用后面的attrs對這個文件的操作進行規范。
上文中的 *.docx diff=pandoc 表示對於所有.docx的文件,在使用diff時,默認是使用[diff "pandoc"] 這個subsection里面的配置。

========================================================================================================================================

Now you can see a pretty coloured diff with the changes you have made to your .docx file since the last commit
git wdiff file.docx

To see all changes over time
git log -p --word-diff=color file.docx

Track changes in Word (.docx) documents getting a diff with the commit.
Automatically when running git commit.
This is only going to work from linux/Mac or Windows running git from a bash shell.

Install pandoc. Pandoc is a program to convert between different file formats. It's going to allow us to convert Word files (.docx) to Markdown (.md).

Set up git hooks to enable automatic generation and tracking of Markdown copies of .docx files.

Copy these hook files to your git project's .git/hooks directory and rename them, or soft-link to them with ln -s, and make them executable (chmod u+x *.sh):

pre-commit-git-diff-docx.sh -> .git/hooks/pre-commit
post-commit-git-diff-docx.sh -> .git/hooks/post-commit

Now every time you run git commit, the pre-commit hook will automatically run before you see the window to enter the log message. The hook is a script that makes a copy in Markdown format (.md) of every .docx file you are committing. The post-commit hook then amends the commit adding the .md files.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM