Fix #25 -- Export src blocks properly

This commit is contained in:
IGARASHI Masanao
2016-12-20 02:29:33 +09:00
parent 6f8f10ac31
commit d4d1e05028

View File

@@ -104,7 +104,7 @@
(:rst-paragraph-spacing nil nil org-rst-paragraph-spacing) (:rst-paragraph-spacing nil nil org-rst-paragraph-spacing)
(:rst-format-drawer-function nil nil org-rst-format-drawer-function) (:rst-format-drawer-function nil nil org-rst-format-drawer-function)
(:rst-format-inlinetask-function nil nil org-rst-format-inlinetask-function) (:rst-format-inlinetask-function nil nil org-rst-format-inlinetask-function)
(:rst-pygments nil nil org-rst-pygments) (:rst-code-block nil nil org-rst-code-block)
(:rst-pygments-langs nil nil org-rst-pygments-langs)) (:rst-pygments-langs nil nil org-rst-pygments-langs))
:filters-alist '((:filter-options . org-rst-math-block-options-filter) :filters-alist '((:filter-options . org-rst-math-block-options-filter)
(:filter-headline . org-rst-filter-headline-blank-lines) (:filter-headline . org-rst-filter-headline-blank-lines)
@@ -274,25 +274,24 @@ The function should return the string to be exported."
;;;; Src blocks ;;;; Src blocks
(defcustom org-rst-pygments t (defcustom org-rst-code-block 'code
"Non-nil means export source code using the pygments package." "The directive used to export SRC-BLOCKs."
:group 'org-export-rst :group 'org-export-rst
:type '(choice :type '(choice
(const :tag "Use pygments" t) (const :tag "Use a code-block directive" code-block)
(const :tag "Export verbatim" nil))) (const :tag "Use a code directive" code)
(const :tag "Use a literal block" nil))
:safe (lambda (s) (memq s '(code-block code nil)))
)
(defcustom org-rst-pygments-langs (defcustom org-rst-pygments-langs
'((emacs-lisp "scheme") (lisp "scheme") (clojure "clojure") '((emacs-lisp "common-lisp") (lisp "common-lisp")
(c "c") (cc "cpp") (cc "c++")
(fortran "fortran") (cperl "perl")
(perl "perl") (cperl "perl") (python "python") (ruby "ruby") (latex "tex")
(html "html") (xml "xml")
(tex "tex") (latex "latex")
(shell-script "bash") (shell-script "bash")
(gnuplot "gnuplot") (caml "ocaml")
(ocaml "ocaml") (caml "ocaml") (sqlite3 "sqlite"))
(sql "sql") (sqlite "sqlite3"))
"Alist mapping languages to their listing language counterpart. "Alist mapping languages to their listing language counterpart.
The key is a symbol, the major mode symbol without the \"-mode\". The key is a symbol, the major mode symbol without the \"-mode\".
The value is the string that should be inserted as the language The value is the string that should be inserted as the language
@@ -1326,37 +1325,44 @@ CONTENTS holds the contents of the item. INFO is a plist holding
contextual information." contextual information."
(when (org-string-nw-p (org-element-property :value src-block)) (when (org-string-nw-p (org-element-property :value src-block))
(let* ((lang (org-element-property :language src-block)) (let* ((lang (org-element-property :language src-block))
(caption (org-element-property :caption src-block))
(label (org-element-property :name src-block)) (label (org-element-property :name src-block))
(value (org-remove-indentation (value (org-remove-indentation
(org-element-property :value src-block))) (org-element-property :value src-block)))
(num-start (org-export-get-loc src-block info)) (num-start (org-export-get-loc src-block info))
(retain-labels (org-element-property :retain-labels src-block)) (codeblockd (plist-get info :rst-code-block))
(attributes (attributes
(org-export-read-attribute :attr_rst src-block)) (org-export-read-attribute :attr_rst src-block))
(class (plist-get attributes :class))) (class (plist-get attributes :class)))
(cond (cond
;; Case 1. No source fontification. ;; Case 1.
((not org-rst-pygments) ((eq codeblockd 'code-block)
(let () (let ((lst-lang
(or (cadr (assq (intern lang) org-rst-pygments-langs)) lang)))
(concat
(format ".. code-block:: %s\n" lst-lang)
(when num-start (format " :lineno-start: %s\n" (1+ num-start)))
(when class (format " :class: %s\n" class))
(when label (format " :name: %s\n" label))
"\n"
(org-rst--indent-string value org-rst-quote-margin))))
;; Case 2. code.
((eq codeblockd 'code)
(let ((lst-lang
(or (cadr (assq (intern lang) org-rst-pygments-langs)) lang)))
(concat
(format ".. code:: %s\n" lst-lang)
(when num-start (format " :number-lines: %s\n" (1+ num-start)))
(when class (format " :class: %s\n" class))
(when label (format " :name: %s\n" label))
"\n"
(org-rst--indent-string value org-rst-quote-margin))))
(t
(concat (concat
"::\n" "::\n"
(when class (format " :class: %s\n" class)) (when class (format " :class: %s\n" class))
(when label (format " :name: %s\n" label)) (when label (format " :name: %s\n" label))
"\n" "\n"
(org-rst--indent-string value org-rst-quote-margin)))) (org-rst--indent-string value org-rst-quote-margin)))))))
;; Case 2. pygments.
(t
(let ((lst-lang
(or (cadr (assq (intern lang) org-rst-pygments-langs)) lang)))
(concat
(format ".. code-block:: %s\n" lst-lang)
(when num-start (format " :number-lines: %s\n" num-start))
(when class (format " :class: %s\n" class))
(when label (format " :name: %s\n" label))
"\n"
(org-rst--indent-string value org-rst-quote-margin)
)))))))
;;;; Statistics Cookie ;;;; Statistics Cookie