From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 0D9mJS/XFWBiEQAA0tVLHw (envelope-from ) for ; Sat, 30 Jan 2021 22:01:19 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id qJw/IS/XFWCuKwAA1q6Kng (envelope-from ) for ; Sat, 30 Jan 2021 22:01:19 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id DC425940366 for ; Sat, 30 Jan 2021 22:01:18 +0000 (UTC) Received: from localhost ([::1]:57702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l5yIu-00028h-6v for larch@yhetil.org; Sat, 30 Jan 2021 17:01:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46176) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l5yIF-00028M-Pn for emacs-orgmode@gnu.org; Sat, 30 Jan 2021 17:00:35 -0500 Received: from mail-qt1-x82c.google.com ([2607:f8b0:4864:20::82c]:37165) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l5yID-0007e4-EL for emacs-orgmode@gnu.org; Sat, 30 Jan 2021 17:00:35 -0500 Received: by mail-qt1-x82c.google.com with SMTP id v3so9517377qtw.4 for ; Sat, 30 Jan 2021 14:00:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=user-agent:from:to:subject:message-id:date:mime-version; bh=DKO4SrsYMgdOswi50SJnPEnWL7jXrOkFzb3r1xCvUKY=; b=cTsJLCxGlnYTGdXGzQ3SUG0QpIDwiaAXmOfIYXI4stQgQpshOBgaQmXjnY6kH/woMo J4Pc5QmWBSIImSvtOdlJ9gIIoCTY1l5lMVmqCLfSULqi240osta+LqfiatxBiZwkI/Lu umknRDhr0uDDizOu3HrNX6M2Qsx6aeVPn1+VmRm80hWbLFGPQQNHSiYMNY76C6FG5bOv iBwJSNKyoazGIT0tslEiZZoWEpVlf6wBnquI6sQi4Y+htpt72P3zyYqAV11KePtUcm5X STrVkeudK8U2chBCnci0fVQ1yHoAjUEOLL/ngy/udRWJt7vputCanf0EXtdVL8VaF+8X 4IDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:user-agent:from:to:subject:message-id:date :mime-version; bh=DKO4SrsYMgdOswi50SJnPEnWL7jXrOkFzb3r1xCvUKY=; b=KkufI7CW8ptj9YYrUF4kbAdzMu93ZwRGo1qtl7MWJtm8+qOEzjnBpitqo8mp653wVK oYjN3Iylg1y4Xn3h/i8HMiuT00TzzQmeT/cxjTO2WsPfannMP1zE4Tgz7BozAKdNIzhI A+IzNeWptVJdIqKruTnYx0zQB3vgBufmgmRYODX1BwNsX61Fd160ksZHW/wTUzMqvaoY OM+5UlOjzh5rXXGw09pxMeg+S5+VJYQZB4xRlfRUraNpryBHxo5tCPNdbBKmIbIIuAQc 4lGJFjXFMkM1jxFKBAWfldecmuWZ3HpIBTRa6+wilsuBy23QFCexpIbGU5HapT+3LDMy pTVA== X-Gm-Message-State: AOAM532J2qJMAgIrNbVoBNuaimYHMUEUQIlZDHrxd4AQTwRzgI4deIzW pw4dggfrsryO05CqAOrEtBkliLcqCIk= X-Google-Smtp-Source: ABdhPJxAkIOQ+g9KSdx5MA2gDdLNnjZ/DcW0AhNb1URSxIM4EkSZF1TcNUSJRwrndbjvTsW5dKOCeQ== X-Received: by 2002:ac8:4f42:: with SMTP id i2mr9771591qtw.92.1612044031727; Sat, 30 Jan 2021 14:00:31 -0800 (PST) Received: from localhost ([190.239.232.181]) by smtp.gmail.com with ESMTPSA id j7sm7007491qtd.36.2021.01.30.14.00.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Jan 2021 14:00:31 -0800 (PST) User-agent: mu4e 1.4.14; emacs 27.1 From: Rodrigo Morales To: emacs-orgmode@gnu.org Subject: [PATCH] ox-md.el export code blocks using grave accents. Message-ID: <87lfcaytom.fsf@gmail.com> Date: Sat, 30 Jan 2021 16:57:13 -0500 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::82c; envelope-from=moralesrodrigo1100@gmail.com; helo=mail-qt1-x82c.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -3.05 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=cTsJLCxG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: DC425940366 X-Spam-Score: -3.05 X-Migadu-Scanner: scn0.migadu.com X-TUID: WrMP/yx23P52 --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: application/vnd.lotus-organizer Content-Disposition: attachment; filename=mre.org Content-Transfer-Encoding: base64 V2hlbiBleHBvcnRpbmcgdGhlIGZvbGxvd2luZyBjb2RlIGJsb2NrIHVzaW5nIGluZGVudGF0aW9u LCBpdCBpcyBub3QKcG9zc2libGUgdG8ga25vdyB3aGVyZSB0aGUgc291cmNlIGNvZGUgZW5kcyBh bmQgdGhlIHJlc3VsdHMgb2YKZXZhbHVhdGlvbiBzdGFydHMuCgojK2JlZ2luX3NyYyBkYXNoCmVj aG8gImVjaG8gYWIiCmVjaG8gInNlcSAxIDIiCiMrZW5kX3NyYwoKIytSRVNVTFRTOgojK2JlZ2lu X2V4YW1wbGUKZWNobyBhYgpzZXEgMSAyCiMrZW5kX2V4YW1wbGUKClRoZSBsYW5ndWFnZSBvZiB0 aGUgZm9sbG93aW5nIGNvZGUgYmxvY2sgd2lsbCBiZSA9c2g9IGJlY2F1c2UgPWRhc2g9CmRvZXNu J3QgYmVsb25nIHRvID1vcmctbWQtbGFuZy1leHBvcnQ9CgojK2JlZ2luX3NyYyBkYXNoCnJlc3Vs dD0wCgpmb3IgaSBpbiAkKHNlcSAxIDUpCmRvCiAgZm9yIGogaW4gJChzZXEgMSA1KQogIGRvCiAg ICByZXN1bHQ9JCgocmVzdWx0ICsgaSArIGopKQogIGRvbmUKZG9uZQoKZWNobyAiJHJlc3VsdCIK IytlbmRfc3JjCgojK1JFU1VMVFM6CiMrYmVnaW5fZXhhbXBsZQoxNTAKIytlbmRfZXhhbXBsZQoK VGhlIGxhbmd1YWdlIG9mIHRoZSBmb2xsb3dpbmcgY29kZSBibG9jayB3aWxsIGJlIHRoZSBzYW1l IGFzIHRoZSBvbmUKdXNlZCBpbiBPcmcgTW9kZSBiZWNhdXNlID1SPSBkb2Vzbid0IGJlbG9uZyB0 byA9b3JnLW1kLWxhbmctZXhwb3J0PQoKIytiZWdpbl9zcmMgUgpkYXRhKExvYmxvbGx5KQoKbWF4 KExvYmxvbGx5ICQgaGVpZ2h0KQojK2VuZF9zcmMKCiMrUkVTVUxUUzoKIytiZWdpbl9leGFtcGxl ClsxXSA2NC4xCiMrZW5kX2V4YW1wbGUK --=-=-= Content-Type: text/markdown Content-Disposition: attachment; filename=mre.md # 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 --=-=-= Content-Type: text/markdown Content-Disposition: attachment; filename=mre-patch.md # 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 ``` --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=patch-export-src-code.diff --- 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. --=-=-= Content-Type: text/plain -- Greetings, Rodrigo Morales. --=-=-=--