Skip to content

Commit

Permalink
add chatu-mode and keybindings for markdown and orgmode
Browse files Browse the repository at this point in the history
  • Loading branch information
kimim committed Feb 10, 2024
1 parent 0bb7f54 commit 93ddea0
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 61 deletions.
107 changes: 92 additions & 15 deletions chatu.el
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
"Define regex of directry. Currently support Chinese charaters."
:type 'string)

(defun chatu-get-keyword (line)
"Get chatu keyword from string LINE."
(when (string-match
"#\\+\\(\\w+\\): +" line)
(list :keyword
(substring-no-properties
(match-string 1 line)))))

(defun chatu-get-type (line)
"Get chatu type from string LINE."
(when (string-match
Expand Down Expand Up @@ -138,7 +146,8 @@
(match-string 1 line)))))

(defvar chatu-keyword-value-functions
'(chatu-get-type
'(chatu-get-keyword
chatu-get-type
chatu-get-input
chatu-get-output
chatu-get-page
Expand All @@ -147,20 +156,21 @@

(defun chatu-normalize-keyword-plist (keyword-plist)
"Normalize KEYWORD-PLIST."
(let* ((input-dir (or (plist-get keyword-plist :input-dir)
chatu-input-dir))
(input (plist-get keyword-plist :input))
(_ (plist-put keyword-plist :input-path (concat input-dir "/" input)))
(output-dir (or (plist-get keyword-plist :output-dir)
chatu-output-dir))
(output-param (plist-get keyword-plist :output))
(page (plist-get keyword-plist :page))
(output (or output-param
(if page
(concat (file-name-sans-extension input) "-" page ".svg")
(file-name-with-extension input "svg"))))
(_ (plist-put keyword-plist :output-path (concat output-dir "/" output))))
keyword-plist))
(when (plist-get keyword-plist :keyword)
(let* ((input-dir (or (plist-get keyword-plist :input-dir)
chatu-input-dir))
(input (plist-get keyword-plist :input))
(_ (plist-put keyword-plist :input-path (concat input-dir "/" input)))
(output-dir (or (plist-get keyword-plist :output-dir)
chatu-output-dir))
(output-param (plist-get keyword-plist :output))
(page (plist-get keyword-plist :page))
(output (or output-param
(if page
(concat (file-name-sans-extension input) "-" page ".svg")
(file-name-with-extension input "svg"))))
(_ (plist-put keyword-plist :output-path (concat output-dir "/" output))))
keyword-plist)))

(defun chatu-keyword-plist ()
"Get normalized KEYWORD-PLIST from string line."
Expand Down Expand Up @@ -206,6 +216,23 @@
(line-end-position))))
(forward-line)))

;;;###autoload
(defun chatu-new ()
"Insert chatu text line respect to mode."
(interactive)
(insert
(cond ((derived-mode-p 'markdown-mode)
"<!-- #+chatu: :")
((derived-mode-p 'org-mode)
"#+chatu: :"))
(read-string "type: " "drawio")
" \""
(read-string "input name: " "")
"\""
(if (derived-mode-p 'markdown-mode)
" -->"
"")))

;;;###autoload
(defun chatu-add ()
"Concert diagram to svg and add to buffer."
Expand Down Expand Up @@ -245,6 +272,56 @@
(funcall (intern (concat "chatu-" type "-open"))
keyword-plist)))

(defun chatu-ctrl-c-ctrl-c ()
"Hook function for `org-ctrl-c-ctrl-c-hook'"
(let ((plist (chatu-keyword-plist)))
(if (string= "chatu" (plist-get plist :keyword))
(progn
(chatu-add)
t)
nil)))

(defun chatu-ctrl-c-ctrl-o (&optional args)
"Hook function for `org-open-at-point-functions'"
(ignore args) ;; args are not used.
(let ((plist (chatu-keyword-plist)))
(if (string= "chatu" (plist-get plist :keyword))
(progn
(chatu-open)
t)
nil)))

;;;###autoload
(define-minor-mode chatu-mode
"Add chatu commands or hooks."
:global nil
:lighter " Chatu"
(if (not chatu-mode)
(cond ((and (featurep 'markdown-mode)
(derived-mode-p 'markdown-mode))
(require 'markdown-mode)
(keymap-unset markdown-mode-map "C-c C-c C-c" t)
(advice-remove 'markdown-follow-thing-at-point
#'chatu-ctrl-c-ctrl-o))
((and (featurep 'org)
(derived-mode-p 'org-mode))
(require 'org)
(remove-hook 'org-ctrl-c-ctrl-c-hook 'chatu-ctrl-c-ctrl-c)
(setq org-open-at-point-functions
(delete 'chatu-ctrl-c-ctrl-o org-open-at-point-functions))))
(cond ((and (featurep 'markdown-mode)
(derived-mode-p 'markdown-mode))
(require 'markdown-mode)
(keymap-set markdown-mode-map "C-c C-c C-c" 'chatu-add)
(advice-add 'markdown-follow-thing-at-point
:before-until
#'chatu-ctrl-c-ctrl-o))
((and (featurep 'org)
(derived-mode-p 'org-mode))
(require 'org)
(add-hook 'org-ctrl-c-ctrl-c-hook 'chatu-ctrl-c-ctrl-c)
(add-to-list 'org-open-at-point-functions 'chatu-ctrl-c-ctrl-o)))))

(provide 'chatu)

;;; chatu.el ends here
118 changes: 73 additions & 45 deletions docs/README.org → docs/README.md
Original file line number Diff line number Diff line change
@@ -1,129 +1,157 @@
* Introduction
# Introduction

A small tool to various files to .svg files or download image files,
and insert it to orgmode or markdown on-the-fly.

*插 chā 图 tú*: insert a diagram.
**插 chā 图 tú**: insert a diagram.

# Install

* Install
Before using chatu, you may need to install some external programs and
Before using `chatu`, you may need to install some external programs and
set them in your PATH:
- draw.io and pdf2svg for drawio file conversion
- plantuml.jar, java for plantuml
- babashka for .bb script
- curl for downloading file from web

From melpa with use-package:
#+begin_src emacs-lisp

```emacs-lisp
(use-package chatu
:hook ((org-mode markdown-mode) . chatu-mode)
:commands (chatu-add
chatu-open)
:custom ((chatu-input-dir "./draws")
(chatu-output-dir "./images")))
#+end_src
```

Or git submodule and use-package
#+begin_src emacs-lisp

```emacs-lisp
(use-package chatu
:load-path "~/.emacs.d/site-lisp/chatu"
:hook ((org-mode markdown-mode) . chatu-mode)
:commands (chatu-add
chatu-open)
:custom ((chatu-input-dir "./draws")
(chatu-output-dir "./images")))
#+end_src
```

# Usage

* Usage
Move cursor to ~chatu~ line, and invoke ~chatu-add~ to add image,
~chatu-open~ to open original .drawio or .puml file.
Move cursor to `chatu` line,
- `C-c C-c` will invoke `chatu-add` to add image in orgmode.
- `C-c C-c C-c` will invoke `chatu-add` to add image in markdown-mode.
- `C-c C-o` will invoke `chatu-open` to open original .drawio or .puml file.

~chatu~ line means different in orgmode and markdown:
- =#+chatu: :drawio= or =#+chatu: :plantuml= in orgmode
- =<-- :drawio -->= or =<-- #+chatu: :plantuml -->= in markdown
`chatu` line means different in orgmode and markdown:
- `#+chatu: :drawio` or `#+chatu: :plantuml` in orgmode
- `<-- #+chatu: :drawio -->` or `<-- #+chatu: :plantuml -->` in markdown

*Remind!*
**Remind!**
- the input files should be the first parameter, for example, .drawio
or .puml file.
- in order to support whitespace in file and dir name, please "quote
all of them".

* Extension
You can easily extend this package by adding new ~chatu-<tool>.el~. For
example, when ~<tool> = drawio~, you need to define ~chatu-drawio-open~ and ~chatu-drawio-script~ in ~chatu-drawio.el~.
# Extension

You can easily extend this package by adding new `chatu-<tool>.el`. For
example, when `<tool> = drawio`, you need to define `chatu-drawio-open` and `chatu-drawio-script` in `chatu-drawio.el`.
- `chatu-drawio-open` is invoked to open the drawio input file at the line.
- `chatu-drawio-script` is used to generate the shell script for conversion.

~chatu-drawio-open~ is invoked to open the drawio input file at the line.
Both method use a `keyword-plist` parameter, which contains the
`chatu` settings from `chatu` line.

~chatu-drawio-script~ is used to generate the shell script for conversion.
```org
#+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg"
```

For example, we can get following `keyword-plist` from above `chatu` line:

```emacs-lisp
(:keyword "chatu" :type "drawio"
:input "diagram.drawio" :output "diagram.svg" :page "0"
:input-dir "./draws" :output-dir "./images"
:input-path "./draws/diagram.drawio"
:output-path "./images/diagram.svg")
```
# Usage

* Usage
See example [[./chatu.org]] in orgmode and [[./chatu.md]] in markdown-mode.
See example [chatu.org](./chatu.org) in orgmode and [chatu.md](./chatu.md) in markdown-mode.

## Simple Usage

** Simple
org-mode:
#+begin_src org
```org
#+chatu: :drawio "diagram.drawio"
#+chatu: :plantuml "diagram.puml"
#+chatu: :curl "http://example.org/image.svg"
#+chatu: :babashka "babashka.bb"
#+end_src
```

markdown-mode:
#+begin_src markdown
```markdown
<!-- #+chatu: :drawio "diagram.drawio" -->
<!-- #+chatu: :plantuml "diagram.puml" -->
<!-- #+chatu: :curl "http://example.org/image.svg" -->
<!-- #+chatu: :babashka "babashka.bb" -->
#+end_src
```

** Omit the extension
## Omit the extension

org-mode:
#+begin_src org
```org
#+chatu: :drawio "diagram"
#+chatu: :plantuml "diagram"
#+end_src
```

markdown-mode:
#+begin_src markdown
```markdown
<!-- #+chatu: :drawio "diagram" -->
<!-- #+chatu: :plantuml "diagram" -->
#+end_src
```

** Add more properties
## Add more properties

org-mode:
#+begin_src org
```org
#+chatu: :drawio "diagram"
#+name: workflow
#+caption: chatu workflow
#+end_src
```

** Extract specific page
## Extract specific page

org-mode:
#+begin_src org
```org
#+chatu: :drawio "diagram.drawio" :page 1
#+chatu: :plantuml "diagram.puml" :page 1
#+end_src
```

markdown-mode:
#+begin_src markdown
```markdown
<!-- #+chatu: :drawio "diagram.drawio" :page 1 -->
<!-- #+chatu: :plantuml "diagram.puml" :page 1 -->
#+end_src
```

## Even more specific

** Even more specific
Add input-dir, output-dir and output file name

org-mode:
#+begin_src org
```org
#+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg"
#+end_src
```

markdown-mode:
#+begin_src markdown
```markdown
<!-- #+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg" -->
#+end_src
```

* Contributors

<a href = "https://github.com/kimim/chatu/graphs/contributors">
<img src = "https://contrib.rocks/image?repo=kimim/chatu"/>
</a>
2 changes: 1 addition & 1 deletion docs/chatu.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
![](images/diagram.svg)

# Insert image generated from plantuml file
<!-- :plantuml "plantuml" -->
<!-- #+chatu: :plantuml "plantuml" -->
![](./images/plantuml.svg)

<!-- :plantuml "plantuml" :page 1 -->
Expand Down

0 comments on commit 93ddea0

Please sign in to comment.