123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- (require 'ox-md)
- (defgroup org-export-gfm nil
- "Options specific to Markdown export back-end."
- :tag "Org Github Flavored Markdown"
- :group 'org-export
- :version "24.4"
- :package-version '(Org . "8.0"))
- (defcustom org-gfm-lang '(("emacs-lisp" . "lisp") ("elisp" . "lisp"))
- "Alist of languages that are not recognized by Github, to
- languages that are. Emacs lisp is a good example of this, where
- we can use lisp as a nice replacement."
- :group 'org-export-gfm)
- (org-export-define-derived-backend 'gfm 'md
- :filters-alist '((:filter-parse-tree . org-md-separate-elements))
- :menu-entry
- '(?g "Export to Github Flavored Markdown"
- ((?G "To temporary buffer"
- (lambda (a s v b) (org-gfm-export-as-markdown a s v)))
- (?g "To file" (lambda (a s v b) (org-gfm-export-to-markdown a s v)))
- (?o "To file and open"
- (lambda (a s v b)
- (if a (org-gfm-export-to-markdown t s v)
- (org-open-file (org-gfm-export-to-markdown nil s v)))))))
- :translate-alist '((inner-template . org-gfm-inner-template)
- (strike-through . org-gfm-strike-through)
- (src-block . org-gfm-src-block)))
- (defun org-gfm-src-block (src-block contents info)
- "Transcode SRC-BLOCK element into Github Flavored Markdown
- format. CONTENTS is nil. INFO is a plist used as a communication
- channel."
- (let* ((lang (org-element-property :language src-block))
- (lang (or (assoc-default lang org-gfm-lang) lang))
- (code (org-export-format-code-default src-block info))
- (prefix (concat "```" lang "\n"))
- (suffix "```"))
- (concat prefix code suffix)))
- (defun org-html-strike-through (strike-through contents info)
- "Transcode STRIKE-THROUGH from Org to Markdown (GFM).
- CONTENTS is the text with strike-through markup. INFO is a plist
- holding contextual information."
- (format "~~%s~~" contents))
- (defun org-gfm-format-toc (headline)
- "Return an appropriate table of contents entry for HEADLINE. INFO is a
- plist used as a communication channel."
- (let* ((title (org-export-data
- (org-export-get-alt-title headline info) info))
- (level (1- (org-element-property :level headline)))
- (indent (concat (make-string (* level 2) ? )))
- (ref-str (replace-regexp-in-string " " "-" (downcase title))))
- (concat indent "- [" title "]" "(#" ref-str ")")))
- (defun org-gfm-inner-template (contents info)
- "Return body of document after converting it to Markdown syntax.
- CONTENTS is the transcoded contents string. INFO is a plist
- holding export options."
- (let* ((depth (plist-get info :with-toc))
- (headlines (and depth (org-export-collect-headlines info depth)))
- (toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") ""))
- (toc-tail (if headlines "\n\n" "")))
- (concat toc-string toc-tail contents)))
- (defun org-gfm-export-as-markdown (&optional async subtreep visible-only)
- "Export current buffer to a Github Flavored Markdown buffer.
- If narrowing is active in the current buffer, only export its
- narrowed part.
- If a region is active, export that region.
- A non-nil optional argument ASYNC means the process should happen
- asynchronously. The resulting buffer should be accessible
- through the `org-export-stack' interface.
- When optional argument SUBTREEP is non-nil, export the sub-tree
- at point, extracting information from the headline properties
- first.
- When optional argument VISIBLE-ONLY is non-nil, don't export
- contents of hidden elements.
- Export is done in a buffer named \"*Org GFM Export*\", which will
- be displayed when `org-export-show-temporary-export-buffer' is
- non-nil."
- (interactive)
- (org-export-to-buffer 'gfm "*Org GFM Export*"
- async subtreep visible-only nil nil (lambda () (text-mode))))
- (defun org-gfm-convert-region-to-md ()
- "Assume the current region has org-mode syntax, and convert it
- to Github Flavored Markdown. This can be used in any buffer.
- For example, you can write an itemized list in org-mode syntax in
- a Markdown buffer and use this command to convert it."
- (interactive)
- (org-export-replace-region-by 'gfm))
- (defun org-gfm-export-to-markdown (&optional async subtreep visible-only)
- "Export current buffer to a Github Flavored Markdown file.
- If narrowing is active in the current buffer, only export its
- narrowed part.
- If a region is active, export that region.
- A non-nil optional argument ASYNC means the process should happen
- asynchronously. The resulting file should be accessible through
- the `org-export-stack' interface.
- When optional argument SUBTREEP is non-nil, export the sub-tree
- at point, extracting information from the headline properties
- first.
- When optional argument VISIBLE-ONLY is non-nil, don't export
- contents of hidden elements.
- Return output file's name."
- (interactive)
- (let ((outfile (org-export-output-file-name ".md" subtreep)))
- (org-export-to-file 'gfm outfile async subtreep visible-only)))
- (provide 'ox-gfm)
|