Converting edraw to svg
Closed this issue · 3 comments
I would like the feature that when C-c C-s is called, the edraw buffer transforms to a link to a file containing the svg of the edraw buffer. Would that be possible? File name should be provided as an input.
Specifically,
[[file:./img/edraw/natsoehunsthaoe]]
Instead of:
[[edraw:data=H4sIAGwElWMAA6WQyw6CMBBFf6UZ11LAR4RQ/gVpKQ3YmlIp/L0jYMSoKxedR2bm3JtmXS+JV9zVDA7HEEgtlKwdg32MTS9sp4xmEAURkOHS6o5B7dw1pdR7H/hdYKykcRiGFEHLSjq0SjffFqMkSeg0hTyzonS/pQcGGMcpVqptGWyqqgLSOWsawUAbLYAozkBwW/jtuSgbac1NcyA0z+R6ZPj41Hs/X8Cc45FFxQjF7DjnqZ0tnDAtRtc+HzX9hxvHL/DpE0wlPvzW/A5xtuxGpQEAAA==]]
Currently, I'm able to save the file as an svg (edraw-editor-export-to-file), but the link remains an edraw link. I'll need to manually insert the image instead.
A simple way will be to have a function that sequentially make this possible:
def save_file_as_svg():
svg_filename = edraw-editor-export-to-file()
filename = relativefile(current_directory, svg_filename)
replace_edraw_link_with_filename(filename)
I'll need 2 help.
- edraw-export-to-file filename don't work for me currently.
- I would like to add C-c C-s to call save-edraw-as-svg (most likely here: https://github.com/misohena/el-easydraw/blob/master/edraw.el#L5056)
;; helper function to open an edraw
(defun open-edraw()
(interactive)
(insert "[[edraw:]]")
(backward-char)
(org-return))
(defun org-remove-link ()
"Remove `org-mode' link at point and trash linked file."
(interactive)
(let* ((link (org-element-context))
(path (org-element-property :path link)))
(delete-region (org-element-property :begin link)
(org-element-property :end link))))
(defun save-edraw-as-svg()
(interactive)
(let (filename (read-file-name (edraw-msg "Write edraw file: ")))
;;(edraw-export-to-file filename) // THIS DONT WORK CURRENTLY
(edraw-org-link-cancel-edit)
(org-remove-link)
(insert (concat "[[file:" filename "]]"))))
As you may already know, there is also an edraw link format that outputs to a file. For example, type the following and C-c C-o will open the editor, edit it and C-c C-c will save it to a file.
[[edraw:file=natsoehunsthaoe.edraw.svg]]
Should you use the file link type?
The ability to edit regular file link types (such as those with the extension .edraw.svg) is planned but not yet implemented.
edraw-mode allows you to edit edraw-created SVG files in a single buffer (but not embedded).
(let ((auto-mode-alist (cons '("." . edraw-mode) auto-mode-alist))) ;;Temporarily ensure that all files are opened in edraw-mode
(find-file "natsoehunsthaoe.edraw.svg")
;; Editor not visible when buffer is empty? Bug?
(when (= (buffer-size) 0)
(with-silent-modifications
(insert " "))))
You can save it with C-x C-s (not C-c C-c).
If you want to edit the file link inline, there is also the following method.
(defun open-file-link-by-edraw ()
(interactive)
(let* ((link (org-element-context))
(path (org-element-property :path link))
(beg (org-element-property :begin link))
(end (org-element-property :end link))
(inline-image-p (org--inline-image-overlays beg end)))
;;TODO: Check link type
;;TODO: Check file extension
;;TODO: Prevent double editing
;;Remove org-mode inline image
(when inline-image-p
(org-toggle-inline-images nil beg end))
;;(edraw-edit-svg is a function added a few days ago and the specifications may change)
(edraw-edit-svg (when (file-exists-p path)
(edraw-svg-read-from-file path))
'edraw-svg
beg end
(lambda (_ok _svg)
;; Restore org-mode inline image
(when inline-image-p
(org-display-inline-images nil t beg end)))
(lambda (svg)
(edraw-svg-write-to-file svg path nil)
t))))
If you just want to write the contents of the edraw link to a file, you can do it like this:
(when-let ((link (edraw-org-link-at-point))
(props (car (edraw-org-link-element-link-properties link nil)))
(data (edraw-org-link-prop-data props)))
;;(edraw-svg-write-to-file (edraw-svg-decode data t) (read-file-name "Write edraw file: ") nil)
;; Or simply decode base64 and gzip and write to file:
(with-temp-file (read-file-name "Write edraw file: ")
(set-buffer-file-coding-system 'utf-8)
(insert data)
(edraw-decode-buffer t)))
If you always use the file link type, the above should help, but perhaps you want to C-c C-s to toggle between the edraw and file link types?
(This comment is translated using Google Translate. I'm sorry if you don't understand the meaning)
(Original Message)
既に知っているかもしれませんがファイルへ出力されるedrawリンク形式もあります。例えば次のように入力してC-c C-oすれば、エディタが開き、編集してC-c C-cすればファイルへ保存されます。
[[edraw:file=natsoehunsthaoe.edraw.svg]]
あなたはfileリンクタイプを使わなければならないのでしょうか?
通常のfileリンクタイプ(拡張子が.edraw.svgであるような)を編集する機能は計画されていますがまだ実装していません。
edraw-modeを使用すればedrawが作成したSVGファイルを単独のバッファで編集できます(埋め込みではありませんが)。
#+begin_src elisp
(let ((auto-mode-alist (cons '("." . edraw-mode) auto-mode-alist))) ;;Temporarily ensure that all files are opened in edraw-mode
(find-file "natsoehunsthaoe.edraw.svg")
;; Editor not visible when buffer is empty? Bug?
(when (= (buffer-size) 0)
(with-silent-modifications
(insert " "))))
#+end_src
C-x C-s (not C-c C-c) で保存できます。
fileリンクをインラインで編集したいなら次のような方法もあります。
#+begin_src elisp
(defun open-file-link-by-edraw ()
(interactive)
(let* ((link (org-element-context))
(path (org-element-property :path link))
(beg (org-element-property :begin link))
(end (org-element-property :end link))
(inline-image-p (org--inline-image-overlays beg end)))
;;TODO: Check link type
;;TODO: Check file extension
;;TODO: Prevent double editing
;;Remove org-mode inline image
(when inline-image-p
(org-toggle-inline-images nil beg end))
;;(edraw-edit-svg is a function added a few days ago and the specifications may change)
(edraw-edit-svg (when (file-exists-p path)
(edraw-svg-read-from-file path))
'edraw-svg
beg end
(lambda (_ok _svg)
;; Restore org-mode inline image
(when inline-image-p
(org-display-inline-images nil t beg end)))
(lambda (svg)
(edraw-svg-write-to-file svg path nil)
t))))
#+end_src
単にedrawリンクの中身をファイルへ書き出したいのであれば次のように出来ます。
#+begin_src elisp
(when-let ((link (edraw-org-link-at-point))
(props (car (edraw-org-link-element-link-properties link nil)))
(data (edraw-org-link-prop-data props)))
;;(edraw-svg-write-to-file (edraw-svg-decode data t) (read-file-name "Write edraw file: ") nil)
;; Or simply decode base64 and gzip and write to file:
(with-temp-file (read-file-name "Write edraw file: ")
(set-buffer-file-coding-system 'utf-8)
(insert data)
(edraw-decode-buffer t)))
#+end_src
常にfileリンクタイプを使うのであれば上のことが助けになると思いますが、ひょっとしてedrawリンクタイプとfileリンクタイプをC-c C-sで切り替えたいという話ですか?
(このコメントはGoogle翻訳を使って翻訳しています。意味が分からなかったら申し訳ありません)
Oh [[edraw:file=natsoehunsthaoe.edraw.svg]]
is beautiful. I can now do the following:
(defun org-mode-open-edraw(&optional filename)
(interactive)
(unless filename
(setq filename (concat "./" (file-relative-name (read-file-name (edraw-msg "Write svg file: ") default-directory) default-directory))))
(insert (concat "[[edraw:file=" filename "]]"))
(backward-char)
(org-return))