| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 | ;;; ox-md.el --- Markdown Back-End for Org Export Engine;; Copyright (C) 2012-2014 Free Software Foundation, Inc.;; Author: Nicolas Goaziou <n.goaziou@gmail.com>;; Keywords: org, wp, markdown;; This file is part of GNU Emacs.;; GNU Emacs is free software: you can redistribute it and/or modify;; it under the terms of the GNU General Public License as published by;; the Free Software Foundation, either version 3 of the License, or;; (at your option) any later version.;; GNU Emacs is distributed in the hope that it will be useful,;; but WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the;; GNU General Public License for more details.;; You should have received a copy of the GNU General Public License;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.;;; Commentary:;; This library implements a Markdown back-end (vanilla flavor) for;; Org exporter, based on `html' back-end.  See Org manual for more;; information.;;; Code:(eval-when-compile (require 'cl))(require 'ox-html);;; User-Configurable Variables(defgroup org-export-md nil  "Options specific to Markdown export back-end."  :tag "Org Markdown"  :group 'org-export  :version "24.4"  :package-version '(Org . "8.0"))(defcustom org-md-headline-style 'atx  "Style used to format headlines.This variable can be set to either `atx' or `setext'."  :group 'org-export-md  :type '(choice	  (const :tag "Use \"atx\" style" atx)	  (const :tag "Use \"Setext\" style" setext)));;; Define Back-End(org-export-define-derived-backend 'md 'html  :export-block '("MD" "MARKDOWN")  :filters-alist '((:filter-parse-tree . org-md-separate-elements))  :menu-entry  '(?m "Export to Markdown"       ((?M "To temporary buffer"	    (lambda (a s v b) (org-md-export-as-markdown a s v)))	(?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))	(?o "To file and open"	    (lambda (a s v b)	      (if a (org-md-export-to-markdown t s v)		(org-open-file (org-md-export-to-markdown nil s v)))))))  :translate-alist '((bold . org-md-bold)		     (code . org-md-verbatim)		     (comment . (lambda (&rest args) ""))		     (comment-block . (lambda (&rest args) ""))		     (example-block . org-md-example-block)		     (fixed-width . org-md-example-block)		     (footnote-definition . ignore)		     (footnote-reference . ignore)		     (headline . org-md-headline)		     (horizontal-rule . org-md-horizontal-rule)		     (inline-src-block . org-md-verbatim)		     (inner-template . org-md-inner-template)		     (italic . org-md-italic)		     (item . org-md-item)		     (line-break . org-md-line-break)		     (link . org-md-link)		     (paragraph . org-md-paragraph)		     (plain-list . org-md-plain-list)		     (plain-text . org-md-plain-text)		     (quote-block . org-md-quote-block)		     (quote-section . org-md-example-block)		     (section . org-md-section)		     (src-block . org-md-example-block)		     (template . org-md-template)		     (verbatim . org-md-verbatim)));;; Filters(defun org-md-separate-elements (tree backend info)  "Fix blank lines between elements.TREE is the parse tree being exported.  BACKEND is the exportback-end used.  INFO is a plist used as a communication channel.Enforce a blank line between elements.  There are threeexceptions to this rule:  1. Preserve blank lines between sibling items in a plain list,  2. Outside of plain lists, preserve blank lines between     a paragraph and a plain list,  3. In an item, remove any blank line before the very first     paragraph and the next sub-list.Assume BACKEND is `md'."  (org-element-map tree (remq 'item org-element-all-elements)    (lambda (e)      (cond       ((not (and (eq (org-element-type e) 'paragraph)		  (eq (org-element-type (org-export-get-next-element e info))		      'plain-list)))	(org-element-put-property e :post-blank 1))       ((not (eq (org-element-type (org-element-property :parent e)) 'item)))       (t (org-element-put-property	   e :post-blank (if (org-export-get-previous-element e info) 1 0))))))  ;; Return updated tree.  tree);;; Transcode Functions;;;; Bold(defun org-md-bold (bold contents info)  "Transcode BOLD object into Markdown format.CONTENTS is the text within bold markup.  INFO is a plist used asa communication channel."  (format "**%s**" contents));;;; Code and Verbatim(defun org-md-verbatim (verbatim contents info)  "Transcode VERBATIM object into Markdown format.CONTENTS is nil.  INFO is a plist used as a communicationchannel."  (let ((value (org-element-property :value verbatim)))    (format (cond ((not (string-match "`" value)) "`%s`")		  ((or (string-match "\\``" value)		       (string-match "`\\'" value))		   "`` %s ``")		  (t "``%s``"))	    value)));;;; Example Block and Src Block(defun org-md-example-block (example-block contents info)  "Transcode EXAMPLE-BLOCK element into Markdown format.CONTENTS is nil.  INFO is a plist used as a communicationchannel."  (replace-regexp-in-string   "^" "    "   (org-remove-indentation    (org-export-format-code-default example-block info))));;;; Headline(defun org-md-headline (headline contents info)  "Transcode HEADLINE element into Markdown format.CONTENTS is the headline contents.  INFO is a plist used asa communication channel."  (unless (org-element-property :footnote-section-p headline)    (let* ((level (org-export-get-relative-level headline info))	   (title (org-export-data (org-element-property :title headline) info))	   (todo (and (plist-get info :with-todo-keywords)		      (let ((todo (org-element-property :todo-keyword							headline)))			(and todo (concat (org-export-data todo info) " ")))))	   (tags (and (plist-get info :with-tags)		      (let ((tag-list (org-export-get-tags headline info)))			(and tag-list			     (format "     :%s:"				     (mapconcat 'identity tag-list ":"))))))	   (priority	    (and (plist-get info :with-priority)		 (let ((char (org-element-property :priority headline)))		   (and char (format "[#%c] " char)))))	   (anchor	    (when (plist-get info :with-toc)	      (org-html--anchor	       (or (org-element-property :CUSTOM_ID headline)		   (concat "sec-"			   (mapconcat 'number-to-string				      (org-export-get-headline-number				       headline info) "-"))))))	   ;; Headline text without tags.	   (heading (concat todo priority title)))      (cond       ;; Cannot create a headline.  Fall-back to a list.       ((or (org-export-low-level-p headline info)	    (not (memq org-md-headline-style '(atx setext)))	    (and (eq org-md-headline-style 'atx) (> level 6))	    (and (eq org-md-headline-style 'setext) (> level 2)))	(let ((bullet	       (if (not (org-export-numbered-headline-p headline info)) "-"		 (concat (number-to-string			  (car (last (org-export-get-headline-number				      headline info))))			 "."))))	  (concat bullet (make-string (- 4 (length bullet)) ? ) heading tags		  "\n\n"		  (and contents		       (replace-regexp-in-string "^" "    " contents)))))       ;; Use "Setext" style.       ((eq org-md-headline-style 'setext)	(concat heading tags anchor "\n"		(make-string (length heading) (if (= level 1) ?= ?-))		"\n\n"		contents))       ;; Use "atx" style.       (t (concat (make-string level ?#) " " heading tags anchor "\n\n" contents))))));;;; Horizontal Rule(defun org-md-horizontal-rule (horizontal-rule contents info)  "Transcode HORIZONTAL-RULE element into Markdown format.CONTENTS is the horizontal rule contents.  INFO is a plist usedas a communication channel."  "---");;;; Italic(defun org-md-italic (italic contents info)  "Transcode ITALIC object into Markdown format.CONTENTS is the text within italic markup.  INFO is a plist usedas a communication channel."  (format "*%s*" contents));;;; Item(defun org-md-item (item contents info)  "Transcode ITEM element into Markdown format.CONTENTS is the item contents.  INFO is a plist used asa communication channel."  (let* ((type (org-element-property :type (org-export-get-parent item)))	 (struct (org-element-property :structure item))	 (bullet (if (not (eq type 'ordered)) "-"		   (concat (number-to-string			    (car (last (org-list-get-item-number					(org-element-property :begin item)					struct					(org-list-prevs-alist struct)					(org-list-parents-alist struct)))))			   "."))))    (concat bullet	    (make-string (- 4 (length bullet)) ? )	    (case (org-element-property :checkbox item)	      (on "[X] ")	      (trans "[-] ")	      (off "[ ] "))	    (let ((tag (org-element-property :tag item)))	      (and tag (format "**%s:** "(org-export-data tag info))))	    (and contents		 (org-trim (replace-regexp-in-string "^" "    " contents))))));;;; Line Break(defun org-md-line-break (line-break contents info)  "Transcode LINE-BREAK object into Markdown format.CONTENTS is nil.  INFO is a plist used as a communicationchannel."  "  \n");;;; Link(defun org-md-link (link contents info)  "Transcode LINE-BREAK object into Markdown format.CONTENTS is the link's description.  INFO is a plist used asa communication channel."  (let ((link-org-files-as-md	 (function	  (lambda (raw-path)	    ;; Treat links to `file.org' as links to `file.md'.	    (if (string= ".org" (downcase (file-name-extension raw-path ".")))		(concat (file-name-sans-extension raw-path) ".md")	      raw-path))))	(type (org-element-property :type link)))    (cond     ((member type '("custom-id" "id"))      (let ((destination (org-export-resolve-id-link link info)))	(if (stringp destination)	; External file.	    (let ((path (funcall link-org-files-as-md destination)))	      (if (not contents) (format "<%s>" path)		(format "[%s](%s)" contents path)))	  (concat	   (and contents (concat contents " "))	   (format "(%s)"		   (format (org-export-translate "See section %s" :html info)			   (mapconcat 'number-to-string				      (org-export-get-headline-number				       destination info)				      ".")))))))     ((org-export-inline-image-p link org-html-inline-image-rules)      (let ((path (let ((raw-path (org-element-property :path link)))		    (if (not (file-name-absolute-p raw-path)) raw-path		      (expand-file-name raw-path))))	    (caption (org-export-data		      (org-export-get-caption		       (org-export-get-parent-element link)) info)))	(format ""		(if (not (org-string-nw-p caption)) path		  (format "%s \"%s\"" path caption)))))     ((string= type "coderef")      (let ((ref (org-element-property :path link)))	(format (org-export-get-coderef-format ref contents)		(org-export-resolve-coderef ref info))))     ((equal type "radio") contents)     ((equal type "fuzzy")      (let ((destination (org-export-resolve-fuzzy-link link info)))	(if (org-string-nw-p contents) contents	  (when destination	    (let ((number (org-export-get-ordinal destination info)))	      (when number		(if (atom number) (number-to-string number)		  (mapconcat 'number-to-string number "."))))))))     ;; Link type is handled by a special function.     ((let ((protocol (nth 2 (assoc type org-link-protocols))))	(and (functionp protocol)	     (funcall protocol		      (org-link-unescape (org-element-property :path link))		      contents		      'md))))     (t (let* ((raw-path (org-element-property :path link))	       (path		(cond		 ((member type '("http" "https" "ftp"))		  (concat type ":" raw-path))		 ((string= type "file")		  (let ((path (funcall link-org-files-as-md raw-path)))		    (if (not (file-name-absolute-p path)) path		      ;; If file path is absolute, prepend it		      ;; with "file:" component.		      (concat "file:" path))))		 (t raw-path))))	  (if (not contents) (format "<%s>" path)	    (format "[%s](%s)" contents path)))))));;;; Paragraph(defun org-md-paragraph (paragraph contents info)  "Transcode PARAGRAPH element into Markdown format.CONTENTS is the paragraph contents.  INFO is a plist used asa communication channel."  (let ((first-object (car (org-element-contents paragraph))))    ;; If paragraph starts with a #, protect it.    (if (and (stringp first-object) (string-match "\\`#" first-object))	(replace-regexp-in-string "\\`#" "\\#" contents nil t)      contents)));;;; Plain List(defun org-md-plain-list (plain-list contents info)  "Transcode PLAIN-LIST element into Markdown format.CONTENTS is the plain-list contents.  INFO is a plist used asa communication channel."  contents);;;; Plain Text(defun org-md-plain-text (text info)  "Transcode a TEXT string into Markdown format.TEXT is the string to transcode.  INFO is a plist holdingcontextual information."  (when (plist-get info :with-smart-quotes)    (setq text (org-export-activate-smart-quotes text :html info)))  ;; Protect ambiguous #.  This will protect # at the beginning of  ;; a line, but not at the beginning of a paragraph.  See  ;; `org-md-paragraph'.  (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))  ;; Protect ambiguous !  (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))  ;; Protect `, *, _ and \  (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))  ;; Handle special strings, if required.  (when (plist-get info :with-special-strings)    (setq text (org-html-convert-special-strings text)))  ;; Handle break preservation, if required.  (when (plist-get info :preserve-breaks)    (setq text (replace-regexp-in-string "[ \t]*\n" "  \n" text)))  ;; Return value.  text);;;; Quote Block(defun org-md-quote-block (quote-block contents info)  "Transcode QUOTE-BLOCK element into Markdown format.CONTENTS is the quote-block contents.  INFO is a plist used asa communication channel."  (replace-regexp-in-string   "^" "> "   (replace-regexp-in-string "\n\\'" "" contents)));;;; Section(defun org-md-section (section contents info)  "Transcode SECTION element into Markdown format.CONTENTS is the section contents.  INFO is a plist used asa communication channel."  contents);;;; Template(defun org-md-inner-template (contents info)  "Return body of document after converting it to Markdown syntax.CONTENTS is the transcoded contents string.  INFO is a plistholding export options."  ;; Make sure CONTENTS is separated from table of contents and  ;; footnotes with at least a blank line.  (org-trim (org-html-inner-template (concat "\n" contents "\n") info)))(defun org-md-template (contents info)  "Return complete document string after Markdown conversion.CONTENTS is the transcoded contents string.  INFO is a plist usedas a communication channel."  contents);;; Interactive function;;;###autoload(defun org-md-export-as-markdown (&optional async subtreep visible-only)  "Export current buffer to a Markdown buffer.If narrowing is active in the current buffer, only export itsnarrowed part.If a region is active, export that region.A non-nil optional argument ASYNC means the process should happenasynchronously.  The resulting buffer should be accessiblethrough the `org-export-stack' interface.When optional argument SUBTREEP is non-nil, export the sub-treeat point, extracting information from the headline propertiesfirst.When optional argument VISIBLE-ONLY is non-nil, don't exportcontents of hidden elements.Export is done in a buffer named \"*Org MD Export*\", which willbe displayed when `org-export-show-temporary-export-buffer' isnon-nil."  (interactive)  (org-export-to-buffer 'md "*Org MD Export*"    async subtreep visible-only nil nil (lambda () (text-mode))));;;###autoload(defun org-md-convert-region-to-md ()  "Assume the current region has org-mode syntax, and convert it to Markdown.This can be used in any buffer.  For example, you can write anitemized list in org-mode syntax in a Markdown buffer and usethis command to convert it."  (interactive)  (org-export-replace-region-by 'md));;;###autoload(defun org-md-export-to-markdown (&optional async subtreep visible-only)  "Export current buffer to a Markdown file.If narrowing is active in the current buffer, only export itsnarrowed part.If a region is active, export that region.A non-nil optional argument ASYNC means the process should happenasynchronously.  The resulting file should be accessible throughthe `org-export-stack' interface.When optional argument SUBTREEP is non-nil, export the sub-treeat point, extracting information from the headline propertiesfirst.When optional argument VISIBLE-ONLY is non-nil, don't exportcontents of hidden elements.Return output file's name."  (interactive)  (let ((outfile (org-export-output-file-name ".md" subtreep)))    (org-export-to-file 'md outfile async subtreep visible-only)))(provide 'ox-md);; Local variables:;; generated-autoload-file: "org-loaddefs.el";; End:;;; ox-md.el ends here
 |