emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] ox-md.el export code blocks using grave accents.


From: Rodrigo Morales
Subject: [PATCH] ox-md.el export code blocks using grave accents.
Date: Sat, 30 Jan 2021 16:57:13 -0500
User-agent: mu4e 1.4.14; emacs 27.1

This patch includes the following changes in =ox-md.el=

+ =org-md-example-block= now exports code blocks using triple grave
  accents instead of four spaces of indentation. This has been done
  for two main reasons:
  
  1. To be able to include the language so that Markdown engines can
     syntax highlight the content of code blocks. Syntax highlighting
     can also occur when using indentation in some websites (see
     [[https://meta.stackexchange.com/questions/184108][this]]. However,
     this method doesn't work in all websites (I haven't found
     information about this on Github.). Therefore, using grave accents
     is more generic.
     
  2. To be able to put the source code and the results of evaluation
     in different code blocks. When using indentation, both the source
     code and the results are shown in the same code block by Markdown
     engines.
     
+ The variable =org-md-lang-export= is now included in order to map
  Org Mode language names to Markdown language names.

The file =mre.org= contains a minimal reproducible example; =mre.md= ,
the resulting file when exporting using the current version; and
=mre-patch.md=, the resulting file when exporting with the changes
of this patch applied.

The patch is shown below.

#+begin_src dash :dir (progn default-directory) :epilogue ":"
gunzip -c /usr/share/emacs/27.1/lisp/org/ox-md.el.gz > ox-md.el
diff -u ox-md.el ox-md-patched.el
#+end_src

#+RESULTS:
#+begin_example
--- ox-md.el    2021-01-30 16:49:33.459042367 -0500
+++ ox-md-patched.el    2021-01-30 16:48:40.232375347 -0500
@@ -50,6 +50,14 @@
          (const :tag "Use \"atx\" style" atx)
          (const :tag "Use \"Setext\" style" setext)))
 
+(defcustom org-md-lang-export
+  '(("dash" . "sh"))
+  "Alist mapping languages to the corresponding language names in Markdown."
+  :group 'org-export-md
+  :type '(repeat
+         (cons
+          (string "Org Mode language name")
+          (string "Markdown language name"))))
 
 ;;;; Footnotes
 
@@ -181,10 +189,24 @@
   "Transcode EXAMPLE-BLOCK element into Markdown format.
 CONTENTS is nil.  INFO is a plist used as a communication
 channel."
-  (replace-regexp-in-string
-   "^" "    "
-   (org-remove-indentation
-    (org-export-format-code-default example-block info))))
+  (let* (language
+        (org-language
+         (plist-get (car (cdr example-block)) :language))
+        (markdown-language
+         (cdr (assoc org-language org-md-lang-export))) ;
+        (content
+         (org-remove-indentation
+          (org-export-format-code-default example-block info))))
+
+    (if markdown-language
+       (setq language markdown-language)
+      (setq language org-language))
+
+    (setq content (replace-regexp-in-string
+                   "\\`" (concat "```" language "\n")
+                   content))
+    
+    (replace-regexp-in-string "\\'" "```" content)))
 
 (defun org-md-export-block (export-block contents info)
   "Transcode a EXPORT-BLOCK element from Org to Markdown.
#+end_example

Attachment: mre.org
Description: Lotus Organizer

Attachment: mre.md
Description: Text Data

Attachment: mre-patch.md
Description: Text Data

Attachment: patch-export-src-code.diff
Description: Text Data

-- 
Greetings,
Rodrigo Morales.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]