emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ox-md.el export code blocks using grave accents
@ 2021-01-29  3:43 Rodrigo Morales
  2021-01-29  3:47 ` Rodrigo Morales
  0 siblings, 1 reply; 10+ messages in thread
From: Rodrigo Morales @ 2021-01-29  3:43 UTC (permalink / raw)
  To: emacs-orgmode


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

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
  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-patched.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 ":"
diff -u ox-md.el ox-md-patched.el
#+end_src

#+RESULTS:
#+begin_example
--- ox-md.el	2021-01-28 22:18:51.566067501 -0500
+++ ox-md-patched.el	2021-01-28 22:14:34.762735829 -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

-- 
Greetings,
Rodrigo Morales.


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [PATCH] ox-md.el export code blocks using grave accents.
@ 2021-01-30 21:57 Rodrigo Morales
  2021-01-30 22:51 ` Tim Cross
  0 siblings, 1 reply; 10+ messages in thread
From: Rodrigo Morales @ 2021-01-30 21:57 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2942 bytes --]


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 #2: mre.org --]
[-- Type: application/vnd.lotus-organizer, Size: 822 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: mre.md --]
[-- Type: text/markdown, Size: 726 bytes --]


# Table of Contents



When exporting the following code block using indentation, it is not
possible to know where the source code ends and the results of
evaluation starts.

    echo "echo ab"
    echo "seq 1 2"

    echo ab
    seq 1 2

The language of the following code block will be `sh` because `dash`
doesn't belong to `org-md-lang-export`

    result=0
    
    for i in $(seq 1 5)
    do
      for j in $(seq 1 5)
      do
        result=$((result + i + j))
      done
    done
    
    echo "$result"

    150

The language of the following code block will be the same as the one
used in Org Mode because `R` doesn't belong to `org-md-lang-export`

    data(Loblolly)
    
    max(Loblolly $ height)

    [1] 64.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: mre-patch.md --]
[-- Type: text/markdown, Size: 699 bytes --]


# Table of Contents



When exporting the following code block using indentation, it is not
possible to know where the source code ends and the results of
evaluation starts.

```sh
echo "echo ab"
echo "seq 1 2"
```

```
echo ab
seq 1 2
```

The language of the following code block will be `sh` because `dash`
doesn't belong to `org-md-lang-export`

```sh
result=0

for i in $(seq 1 5)
do
  for j in $(seq 1 5)
  do
    result=$((result + i + j))
  done
done

echo "$result"
```

```
150
```

The language of the following code block will be the same as the one
used in Org Mode because `R` doesn't belong to `org-md-lang-export`

```R
data(Loblolly)

max(Loblolly $ height)
```

```
[1] 64.1
```


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: patch-export-src-code.diff --]
[-- Type: text/x-patch, Size: 1460 bytes --]

--- ox-md.el	2021-01-28 22:18:51.566067501 -0500
+++ ox-md-patched.el	2021-01-28 22:14:34.762735829 -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.

[-- Attachment #6: Type: text/plain, Size: 33 bytes --]


-- 
Greetings,
Rodrigo Morales.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-04-27 11:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29  3:43 [PATCH] ox-md.el export code blocks using grave accents Rodrigo Morales
2021-01-29  3:47 ` Rodrigo Morales
2021-01-31 19:55   ` Rodrigo Morales
2021-01-30 21:57 Rodrigo Morales
2021-01-30 22:51 ` Tim Cross
2021-01-30 23:53   ` Rodrigo Morales
2021-04-27  8:43   ` Bastien
2021-04-27 10:03   ` Bruce D'Arcus
2021-04-27 10:18     ` Timothy
2021-04-27 11:32       ` Nicolas Goaziou

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).