emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] [BUG] Support attr_html in source code and fixed-width blocks
@ 2024-06-18  2:55 Suhail Singh
  2024-06-18 15:33 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Suhail Singh @ 2024-06-18  2:55 UTC (permalink / raw)
  To: Org mailing list

[-- 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


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

* Re: [PATCH] [BUG] Support attr_html in source code and fixed-width blocks
  2024-06-18  2:55 [PATCH] [BUG] Support attr_html in source code and fixed-width blocks Suhail Singh
@ 2024-06-18 15:33 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2024-06-18 15:33 UTC (permalink / raw)
  To: Suhail Singh, TEC; +Cc: Org mailing list

Suhail Singh <suhailsingh247@gmail.com> writes:

> Unlike example blocks, source code and fixed-width blocks don't support
> the attr_html keyword.  Contrast these outputs:
> ...
> 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).

Sounds reasonable.
Timothy, may you have a look?
I am not sure if we want to add all the attributes to every transcoded
element. At least in some cases, we do discard them
(`org-html--textarea-block').

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

end of thread, other threads:[~2024-06-18 15:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-18  2:55 [PATCH] [BUG] Support attr_html in source code and fixed-width blocks Suhail Singh
2024-06-18 15:33 ` Ihor Radchenko

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).