emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Suhail Singh <suhailsingh247@gmail.com>
To: Org mailing list <emacs-orgmode@gnu.org>
Subject: [PATCH] [BUG] Support attr_html in source code and fixed-width blocks
Date: Mon, 17 Jun 2024 22:55:28 -0400	[thread overview]
Message-ID: <87v8277ye7.fsf@gmail.com> (raw)

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

Unlike example blocks, source code and fixed-width blocks don't support
the attr_html keyword.  Contrast these outputs:

#+begin_src emacs-lisp :results value replace :wrap src html
  (require 'org)
  (require 'ox-html)

  (org-export-string-as
   "#+attr_html: :class foo
  ,#+begin_src sh :exports code
    pwd
  ,#+end_src"
   'html t)
#+end_src

#+RESULTS:
#+begin_src html
<div class="org-src-container">
<pre class="src src-sh"><span style="color: #e090d7;">pwd</span>
</pre>
</div>
#+end_src

#+begin_src emacs-lisp :results value replace :wrap src html
  (require 'org)
  (require 'ox-html)

  (org-export-string-as
   "#+attr_html: :class foo
  ,#+RESULTS:
  : blah"
   'html t)
#+end_src

#+RESULTS:
#+begin_src html
<pre class="example">
blah
</pre>
#+end_src


With the output for example blocks:

#+begin_src emacs-lisp :results value replace :wrap src html
  (require 'org)
  (require 'ox-html)

  (org-export-string-as
   "#+attr_html: :class foo
  ,#+begin_example
    hello world!
  ,#+end_example"
   'html t)
#+end_src

#+RESULTS:
#+begin_src html
<pre class="example foo" id="org2d25618">
hello world!
</pre>
#+end_src


The attached patches are a straight-forward copy-paste of relevant code
from org-html-example-block.  It may be better to refactor this logic
and ensure that it is applied on all relevant AST nodes (others are
probably affected as well).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-html-Add-support-for-attr_html-for-source-code-bl.patch --]
[-- Type: text/x-patch, Size: 2019 bytes --]

From 7a61bb6a7c7e1122199232a1c01885fb270dd370 Mon Sep 17 00:00:00 2001
From: Suhail <suhail@bayesians.ca>
Date: Mon, 17 Jun 2024 21:04:18 -0400
Subject: [PATCH 1/2] ox-html: Add support for attr_html for source code blocks

* lisp/ox-html.el (org-html-src-block): Handle attr_html in a manner
similar to example blocks.

TINYCHANGE
---
 lisp/ox-html.el | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index d1687cf5a..675d85ffe 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -3667,14 +3667,24 @@ (defun org-html-src-block
 contextual information."
   (if (org-export-read-attribute :attr_html src-block :textarea)
       (org-html--textarea-block src-block)
-    (let* ((lang (org-element-property :language src-block))
+    (let* ((attributes (org-export-read-attribute :attr_html src-block))
+           (lang (org-element-property :language src-block))
 	   (code (org-html-format-code src-block info))
 	   (label (let ((lbl (org-html--reference src-block info t)))
 		    (if lbl (format " id=\"%s\"" lbl) "")))
 	   (klipsify  (and  (plist-get info :html-klipsify-src)
                             (member lang '("javascript" "js"
 					   "ruby" "scheme" "clojure" "php" "html")))))
-      (format "<div class=\"org-src-container\">\n%s%s\n</div>"
+      (if-let ((class-val (plist-get attributes :class)))
+          (setq attributes (plist-put attributes :class (concat "org-src-container " class-val)))
+        (setq attributes (plist-put attributes :class "org-src-container")))
+      (format "<div%s>\n%s%s\n</div>"
+              (let* ((reference (org-html--reference src-block info))
+		     (a (org-html--make-attribute-string
+			 (if (or (not reference) (plist-member attributes :id))
+			     attributes
+			   (plist-put attributes :id reference)))))
+		(if (org-string-nw-p a) (concat " " a) ""))
 	      ;; Build caption.
 	      (let ((caption (org-export-get-caption src-block)))
 		(if (not caption) ""
-- 
2.45.2


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-html-Add-support-for-attr_html-in-fixed-width-blo.patch --]
[-- Type: text/x-patch, Size: 1796 bytes --]

From 5d4fa5c3e0a7933d10d8f1ece3e368d35e4838ad Mon Sep 17 00:00:00 2001
From: Suhail <suhail@bayesians.ca>
Date: Mon, 17 Jun 2024 22:00:35 -0400
Subject: [PATCH 2/2] ox-html: Add support for attr_html in fixed-width blocks

* lisp/ox-html.el (org-html-fixed-width): Handle attr_html in a manner
similar to example blocks.

TINYCHANGE
---
 lisp/ox-html.el | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 675d85ffe..848017f1d 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2728,13 +2728,23 @@ (defun org-html-export-block
 
 ;;;; Fixed Width
 
-(defun org-html-fixed-width (fixed-width _contents _info)
+(defun org-html-fixed-width (fixed-width _contents info)
   "Transcode a FIXED-WIDTH element from Org to HTML.
 CONTENTS is nil.  INFO is a plist holding contextual information."
-  (format "<pre class=\"example\">\n%s</pre>"
-	  (org-html-do-format-code
-	   (org-remove-indentation
-	    (org-element-property :value fixed-width)))))
+  (let ((attributes (org-export-read-attribute :attr_html fixed-width)))
+    (if-let ((class-val (plist-get attributes :class)))
+        (setq attributes (plist-put attributes :class (concat "example " class-val)))
+      (setq attributes (plist-put attributes :class "example")))
+    (format "<pre%s>\n%s</pre>"
+            (let* ((reference (org-html--reference fixed-width info))
+		   (a (org-html--make-attribute-string
+		       (if (or (not reference) (plist-member attributes :id))
+			   attributes
+			 (plist-put attributes :id reference)))))
+	      (if (org-string-nw-p a) (concat " " a) ""))
+	    (org-html-do-format-code
+	     (org-remove-indentation
+	      (org-element-property :value fixed-width))))))
 
 ;;;; Footnote Reference
 
-- 
2.45.2


[-- Attachment #4: Type: text/plain, Size: 309 bytes --]



Suhail (2):
  ox-html: Add support for attr_html for source code blocks
  ox-html: Add support for attr_html in fixed-width blocks

 lisp/ox-html.el | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)


base-commit: e666660c7d9fa3a74a7f5246d72e6020ab33cfd2
-- 
2.45.2


             reply	other threads:[~2024-06-18  2:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-18  2:55 Suhail Singh [this message]
2024-06-18 15:33 ` [PATCH] [BUG] Support attr_html in source code and fixed-width blocks Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v8277ye7.fsf@gmail.com \
    --to=suhailsingh247@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).