🏡 index : pandoc.el.git

author Jacob Walchuk <yaqub@Jacobs-MacBook-Air-2.local> 2024-03-19 16:11:25.0 +00:00:00
committer Jacob Walchuk <yaqub@Jacobs-MacBook-Air-2.local> 2024-03-19 16:11:25.0 +00:00:00
commit
4a8be5f25c4282ac0d6f61b107f5d72905959da5 [patch]
tree
a33b6d377b18d10006489d1acaf79348771c0d65
download
4a8be5f25c4282ac0d6f61b107f5d72905959da5.tar.gz

initial commit



Diff

 pandoc.el | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 readme.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 181 insertions(+)

diff --git a/pandoc.el b/pandoc.el
new file mode 100644
index 0000000..19307ef 100644
--- /dev/null
+++ a/pandoc.el
@@ -1,0 +1,84 @@
;;; pandoc.el --- Pandoc integration for Emacs

;; Author: ChatGPT 
;; Version: 0.1
;; Package-Requires: ((emacs "24.3"))

;;; Commentary:
;; This package provides easy Pandoc integration for Emacs, allowing users to
;; convert files to various formats with optional citation support.

;;; Code:

(defgroup pandoc nil
  "Integration with Pandoc for various document conversions."
  :prefix "pandoc-"
  :group 'external)

(defcustom pandoc-citation-styles-dir "~/.pandoc/csl/"
  "Directory where Pandoc citation style files (.csl) are stored."
  :type 'directory
  :group 'pandoc)

(defcustom pandoc-default-citation-style "apa.csl"
  "Default citation style."
  :type 'string
  :group 'pandoc)

(defun pandoc--convert (src-file dest-format &optional citation-style)
  "Convert SRC-FILE to DEST-FORMAT using Pandoc.
Optionally use CITATION-STYLE if provided."
  (let* ((dest-file (concat (file-name-sans-extension src-file) "." dest-format))
         (command (concat "pandoc " src-file " -o " dest-file)))
    (when citation-style
      (setq command (concat command " --csl=" (expand-file-name citation-style pandoc-citation-styles-dir))))
    (shell-command command)
    dest-file))

(defun pandoc-view-as (format &optional with-citations)
  "Convert current file to FORMAT and open it. Use citations if WITH-CITATIONS is non-nil."
  (interactive "sFormat: \nP")
  (let* ((src-file (buffer-file-name))
         (citation-style (if with-citations
                             (completing-read "Citation Style: " (directory-files pandoc-citation-styles-dir nil "\\.csl$") nil t)
                           pandoc-default-citation-style)) ;; Use default citation style if with-citations is non-nil but no style is selected.
         (dest-file (pandoc--convert src-file format citation-style)))
    (if (and dest-file (file-exists-p dest-file))
        (cond
         ((string-equal format "docx") (shell-command (concat "open -a 'Microsoft Word' " dest-file)))
         ((string-equal format "pdf") (shell-command (concat "open " dest-file)))
         (t (shell-command (concat "open " dest-file))))
      (message "Conversion failed or file does not exist."))))

(defun pandoc-view-in-word (&optional with-citations)
  "Convert current file to a DOCX using Pandoc and open it in Microsoft Word. With prefix argument, use citations."
  (interactive "P")
  (pandoc-view-as "docx" with-citations))

(defun pandoc-view-as-latex (&optional with-citations)
  "Convert current file to PDF using Pandoc with LaTeX and open it. With prefix argument, use citations."
  (interactive "P")
  (pandoc-view-as "pdf" with-citations))

(defun pandoc-view-as-html (&optional with-citations)
  "Convert current file to HTML using Pandoc and open it in the default browser. With prefix argument, use citations."
  (interactive "P")
  (pandoc-view-as "html" with-citations))

(defun pandoc-view-as-markdown (&optional with-citations)
  "Convert current file to Markdown using Pandoc and open it. With prefix argument, use citations."
  (interactive "P")
  (pandoc-view-as "md" with-citations))

;;; Keybindings
(global-set-key (kbd "M-p w") 'pandoc-view-in-word)
(global-set-key (kbd "M-p W") (lambda () (interactive) (pandoc-view-in-word t)))
(global-set-key (kbd "M-p l") 'pandoc-view-as-latex)
(global-set-key (kbd "M-p L") (lambda () (interactive) (pandoc-view-as-latex t)))
(global-set-key (kbd "M-p h") 'pandoc-view-as-html)
(global-set-key (kbd "M-p H") (lambda () (interactive) (pandoc-view-as-html t)))
(global-set-key (kbd "M-p m") 'pandoc-view-as-markdown)
(global-set-key (kbd "M-p M") (lambda () (interactive) (pandoc-view-as-markdown t)))

(provide 'pandoc)
;;; pandoc.el ends here
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..0554787 100644
--- /dev/null
+++ a/readme.md
@@ -1,0 +1,97 @@
# pandoc.el

An Emacs package for integrating Pandoc, allowing for easy conversion of documents to various formats directly within Emacs. This package provides functions to convert and view documents in formats such as DOCX, PDF (via LaTeX), HTML, and Markdown. It supports optional citation styles for academic and professional document formatting. This package was created with the assistance of OpenAI's ChatGPT.

## Features

- Convert documents to multiple formats: DOCX, PDF, HTML, and Markdown.
- Optional support for citations across all formats, with customizable citation styles.
- Integration with Microsoft Word for DOCX viewing (macOS only), and system default applications for PDF, HTML, and Markdown.
- Customizable citation styles directory and default citation style.
- Easy-to-use interactive functions.
- Default keybindings for quick access to conversion functions.

## Installation

1. Ensure Pandoc is installed on your system. If not, please install Pandoc from [the Pandoc website](https://pandoc.org/installing.html) or use a package manager.
2. Download `pandoc.el` and place it in your Emacs `load-path`.
3. Add `(require 'pandoc)` to your Emacs configuration file (e.g., `init.el` or `.emacs`).

### Using straight.el

To install `pandoc.el` using `straight.el`, add the following to your Emacs configuration file (e.g., `init.el` or `.emacs`):

```emacs-lisp

(straight-use-package
 '(pandoc :type git :host github :repo "yourusername/pandoc.el"))```

### Customization

To include instructions for installing pandoc.el using straight.el and customizing the citation styles directory and default citation style, you can amend the README.md like this:

markdown

# pandoc.el

An Emacs package for integrating Pandoc, allowing for easy conversion of documents to various formats directly within Emacs. This package provides functions to convert and view documents in formats such as DOCX, PDF (via LaTeX), HTML, and Markdown. It supports optional citation styles for academic and professional document formatting. This package was created with the assistance of OpenAI's ChatGPT.

## Features

- Convert documents to multiple formats: DOCX, PDF, HTML, and Markdown.
- Optional support for citations across all formats, with customizable citation styles.
- Integration with Microsoft Word for DOCX viewing (macOS only), and system default applications for PDF, HTML, and Markdown.
- Customizable citation styles directory and default citation style.
- Easy-to-use interactive functions.
- Default keybindings for quick access to conversion functions.

## Installation

### Prerequisites

Ensure Pandoc is installed on your system. If not, please install Pandoc from [the Pandoc website](https://pandoc.org/installing.html) or use a package manager.

### Using straight.el

To install `pandoc.el` using `straight.el`, add the following to your Emacs configuration file (e.g., `init.el` or `.emacs`):

```emacs-lisp

(straight-use-package
 '(pandoc :type git :host github :repo "yourusername/pandoc.el"))

Customization

After installation, customize the citation styles directory and default citation style by adding the following to your Emacs configuration:

```emacs-lisp

(setq pandoc-citation-styles-dir "/path/to/your/citation/styles/")
(setq pandoc-default-citation-style "your-default-style.csl")```

## Usage

This package provides several interactive functions, which you can call with `M-x` followed by the function name:

- `pandoc-view-in-word`: Convert the current file to DOCX format and open it in Microsoft Word. Add a prefix argument (`C-u`) to include citations.
- `pandoc-view-as-latex`: Convert the current file to PDF using LaTeX and open it. Add a prefix argument to include citations.
- `pandoc-view-as-html`: Convert the current file to HTML and open it in the default web browser. Add a prefix argument to include citations.
- `pandoc-view-as-markdown`: Convert the current file to Markdown and open it. Add a prefix argument to include citations.

### Customization

You can customize the directory where Pandoc citation style files (.csl) are stored and the default citation style by setting `pandoc-citation-styles-dir` and `pandoc-default-citation-style` respectively in your Emacs configuration.

### Keybindings

Default keybindings are provided for convenience:

- `M-p w`: View in Word without citations.
- `M-p W`: View in Word with citations.
- `M-p l`: View as LaTeX (PDF) without citations.
- `M-p L`: View as LaTeX (PDF) with citations.
- `M-p h`: View as HTML without citations.
- `M-p H`: View as HTML with citations.
- `M-p m`: View as Markdown without citations.
- `M-p M`: View as Markdown with citations.

## Acknowledgments

This package was created with the assistance of OpenAI's ChatGPT. It is intended for educational and practical use, with hopes that it enhances your productivity within Emacs.