* lisp/ox-texinfo.el (org-texinfo-src-block): Preserve the language
field during export.
* etc/ORG-NEWS (Version 9.8): Add news entry.
* testing/lisp/test-ox-texinfo.el: Add tests.
---
Thanks for the feedback!

Rudolf Adamkovič <rud...@adamkovic.org> writes:
> Ha!  I have the exact same patch in works.  I also asked on the Texinfo
> Help mailing list [1] whether this will break compatibility with older
> Texinfo versions (like @math did, where we had to add a special check).
> The good news is, it will not.

My skills at submitting patches other people are working on are
unparalleled.

> I think it would be useful to mention "syntax highlighting", even link
> the relevant Texinfo documentation, given that is the main motivation
> behind the feature.
>
> Nitpick: "no longer removes" could be positive, e.g. "now includes".

> Great work!  Would you be willing to also add some tests for this logic
> (Lisp, non-Lisp, and no-language blocks), so we can freely refactor in
> the future?

Done and done and done.

Ihor Radchenko <yanta...@posteo.net> writes:
> Note, however, that
> https://www.gnu.org/software/texinfo/manual/texinfo/texinfo.html#Syntax-Highlighting
> may or may not understand the src block language names as they are
> used in Org. We may want some kind of dictionary to translate Org's
> language to language supported by highlight/pygments/source-highlight.
> Something akin `org-latex-listings-langs'.

I added org-texinfo-listings-langs. I'm not super familiar with the Org
codebase so I might've missed something, but as far as I can tell it's
functionally correct.



 etc/ORG-NEWS                    | 28 +++++++++++++
 lisp/ox-texinfo.el              | 41 +++++++++++++++++--
 testing/lisp/test-ox-texinfo.el | 70 +++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 4 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 05280f3de..a8120100c 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -320,6 +320,34 @@ behaviour of other exporters. In this case, to exclude a 
section from
 the table of contents, mark it as =:UNNUMBERED: notoc= in its
 properties.
 
+*** Texinfo exporter now preserves source block language during export
+
+The Texinfo exporter now includes the language from source blocks
+during export.
+
+For example, exporting the following to Texinfo
+
+#+begin_src org
+,#+begin_src ruby
+puts "Hello World!"
+,#+end_src
+#+end_src
+
+will result in
+
+#+begin_src texinfo
+@example ruby
+puts "Hello World!"
+@end example
+#+end_src
+
+When combined with the syntax highlighting feature introduced in
+Texinfo 7.1, this allows for documentation generated from Texinfo
+files to have syntax highlighting.
+
+The Texinfo language can be changed by customizing
+~org-texinfo-listings-langs~.
+
 ** New functions and changes in function arguments
 
 # This also includes changes in function behavior from Elisp perspective.
diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el
index a99656c3c..40ee382a1 100644
--- a/lisp/ox-texinfo.el
+++ b/lisp/ox-texinfo.el
@@ -123,6 +123,7 @@
     (:texinfo-inactive-timestamp-format nil nil 
org-texinfo-inactive-timestamp-format)
     (:texinfo-diary-timestamp-format nil nil 
org-texinfo-diary-timestamp-format)
     (:texinfo-link-with-unknown-path-format nil nil 
org-texinfo-link-with-unknown-path-format)
+    (:texinfo-listings-langs nil nil org-texinfo-listings-langs)
     (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
     (:texinfo-table-scientific-notation nil nil 
org-texinfo-table-scientific-notation)
     (:texinfo-table-default-markup nil nil org-texinfo-table-default-markup)
@@ -411,6 +412,30 @@ set `org-texinfo-logfiles-extensions'."
   :group 'org-export-latex
   :type 'boolean)
 
+;;;; Src blocks
+
+(defcustom org-texinfo-listings-langs
+  '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
+    (cc "C++")
+    (cperl "Perl") (python "Python")
+    (shell-script "bash")
+    (sqlite "sql")
+    (makefile "make"))
+  "Alist mapping languages to their listing language counterpart.
+The key is a symbol, the major mode symbol without the \"-mode\".
+The value is the string that should be inserted as the language
+parameter for the @example environment.  If the mode name and the
+listings name are the same, the language does not need an entry
+in this list - but it does not hurt if it is present.
+
+The desired mapping may change depending on what syntax
+highlighting package, if any, will be used in Texinfo."
+  :package-version '(Org . "9.8")
+  :type '(repeat
+         (list
+          (symbol :tag "Major mode       ")
+          (string :tag "Listings language"))))
+
 ;;; Constants
 
 (defconst org-texinfo-max-toc-depth 4
@@ -1745,13 +1770,21 @@ as a communication channel."
   "Transcode a SRC-BLOCK element from Org to Texinfo.
 CONTENTS holds the contents of the item.  INFO is a plist holding
 contextual information."
-  (let* ((lisp (string-match-p
-                "lisp"
-               (or (org-element-property :language src-block) "")))
+  (let* ((lang (org-element-property :language src-block))
+         (lst-lang (or (and lang
+                            (cadr (assq (intern lang)
+                                        (plist-get info 
:texinfo-listings-langs))))
+                       lang))
+         (lisp (and lst-lang (string-match-p "lisp" lst-lang)))
         (code (org-texinfo--sanitize-content
                (org-export-format-code-default src-block info)))
         (value (format
-                (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
+                 (cond
+                  (lisp "@lisp\n%s@end lisp")
+                  (lst-lang
+                   (format "@example %s\n%%s@end example" lst-lang))
+                  ;; Language is mandatory but allow nil for compatibility.
+                  (t "@example\n%s@end example"))
                 code))
         (caption (org-export-get-caption src-block))
         (shortcaption (org-export-get-caption src-block t)))
diff --git a/testing/lisp/test-ox-texinfo.el b/testing/lisp/test-ox-texinfo.el
index e34a2137f..6bbd4ce37 100644
--- a/testing/lisp/test-ox-texinfo.el
+++ b/testing/lisp/test-ox-texinfo.el
@@ -629,5 +629,75 @@ Headings exported as list items have no such problem."
           (re-search-forward "^@anchor{Heading 2-2-2}$")
           (re-search-forward "^@subheading Heading 2-2-2$")))))))
 
+
+;;; Src blocks
+
+(ert-deftest test-ox-texinfo/src-blocks-no-language ()
+  "Test that source blocks without a language are exported correctly."
+  (should
+   (org-test-with-temp-text
+    "#+begin_src\n'(1 2 3)\n#+end_src"
+    (let ((export-buffer "*Test Texinfo Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'texinfo export-buffer
+        nil nil nil nil nil
+        #'texinfo-mode)
+      (with-current-buffer export-buffer
+        (goto-char (point-min))
+        (and
+         (re-search-forward "^@example$")
+         (re-search-forward "^@end example$")))))))
+
+(ert-deftest test-ox-texinfo/src-blocks-non-lisp-language ()
+  "Test that source blocks with languages that don't contain the
+substring lisp are exported correctly."
+  (should
+   (org-test-with-temp-text
+    "#+begin_src scheme\n'(1 2 3)\n#+end_src"
+    (let ((export-buffer "*Test Texinfo Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'texinfo export-buffer
+        nil nil nil nil nil
+        #'texinfo-mode)
+      (with-current-buffer export-buffer
+        (goto-char (point-min))
+        (and
+         (re-search-forward "^@example scheme$")
+         (re-search-forward "^@end example$")))))))
+
+(ert-deftest test-ox-texinfo/src-blocks-lisp-language ()
+  "Test that source blocks with languages that do contain the
+substring lisp are exported correctly."
+  (should
+   (org-test-with-temp-text
+    "#+begin_src lisp\n'(1 2 3)\n#+end_src"
+    (let ((export-buffer "*Test Texinfo Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'texinfo export-buffer
+        nil nil nil nil nil
+        #'texinfo-mode)
+      (with-current-buffer export-buffer
+        (goto-char (point-min))
+        (and
+         (re-search-forward "^@lisp$")
+         (re-search-forward "^@end lisp$")))))))
+
+(ert-deftest test-ox-texinfo/src-blocks-listing-language ()
+  "Test that source blocks with languages mapped in
+org-texinfo-listing-langs are renamed properly."
+  (should
+   (org-test-with-temp-text
+    "#+begin_src shell-script\necho Hello World\n#+end_src"
+    (let ((export-buffer "*Test Texinfo Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'texinfo export-buffer
+        nil nil nil nil nil
+        #'texinfo-mode)
+      (with-current-buffer export-buffer
+        (goto-char (point-min))
+        (and
+         (re-search-forward "^@example bash$")
+         (re-search-forward "^@end example$")))))))
+
 (provide 'test-ox-texinfo)
 ;;; test-ox-texinfo.el end here
-- 
2.48.1


Reply via email to