Ver código fonte

org-e-html/org-e-odt: Support for code numbering

Jambunathan K 13 anos atrás
pai
commit
b65725e039
2 arquivos alterados com 83 adições e 69 exclusões
  1. 40 32
      EXPERIMENTAL/org-e-html.el
  2. 43 37
      EXPERIMENTAL/org-e-odt.el

+ 40 - 32
EXPERIMENTAL/org-e-html.el

@@ -1855,35 +1855,33 @@ contextual information."
 ;; (defun org-odt-format-source-code-or-example-colored
 ;;   (lines lang caption textareap cols rows num cont rpllbl fmt))
 
+(defun org-e-html-format-source-line-with-line-number-and-label (line)
+  (let ((ref (org-find-text-property-in-string 'org-coderef line))
+	(num (org-find-text-property-in-string 'org-loc line)))
+    (when num
+      (setq line (format "<span class=\"linenr1\">%d:  </span>%s (%s)"
+			 num line ref)))
+    (when ref
+      (setq line
+	    (format
+	     "<span id=\"coderef-%s\" class=\"coderef-off\">%s (%s)</span>"
+	     ref line ref)))
+    line))
+
 (defun org-e-html-format-source-code-or-example-plain
   (lines lang caption textareap cols rows num cont rpllbl fmt)
-  (setq lines
-	(concat
-	 "<pre class=\"example\">\n"
-	 (cond
-	  (textareap
-	   (concat
-	    (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
-		    cols rows)
-	    lines "</textarea>\n</p>\n"))
-	  (t
-	   (with-temp-buffer
-	     (insert lines)
-	     (goto-char (point-min))
-	     (while (re-search-forward "[<>&]" nil t)
-	       (replace-match (cdr (assq (char-before)
-					 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
-			      t t))
-	     (buffer-string))))
-	 "</pre>\n"))
-
-  (unless textareap
-    (setq lines (org-export-number-lines lines 1 1 num cont rpllbl fmt)))
-
-  ;; (when (string-match "\\(\\`<[^>]*>\\)\n" lines)
-  ;;   (setq lines (replace-match "\\1" t nil lines)))
-
-  lines)
+  (format
+   "\n<pre class=\"example\">\n%s\n</pre>"
+   (cond
+    (textareap
+     (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">%s\n</textarea>\n</p>\n"
+	     cols rows lines))
+    (t (mapconcat
+	(lambda (line)
+	  (org-e-html-format-source-line-with-line-number-and-label
+	   (org-e-html-encode-plain-text line)))
+	(org-split-string lines "\n")
+	"\n")))))
 
 (defun org-e-html-format-source-code-or-example-colored
   (lines lang caption textareap cols rows num cont rpllbl fmt)
@@ -1925,7 +1923,11 @@ contextual information."
 	     lines "</div>")))
 
     (unless textareap
-      (setq lines (org-export-number-lines lines 1 1 num cont rpllbl fmt)))
+      (setq lines
+	    (mapconcat
+	     (lambda (line)
+	       (org-e-html-format-source-line-with-line-number-and-label line))
+	     (org-split-string lines "\n") "\n")))
 
     ;; (when (string-match "\\(\\`<[^>]*>\\)\n" lines)
     ;;   (setq lines (replace-match "\\1" t nil lines)))
@@ -2008,7 +2010,7 @@ INDENT was the original indentation of the block."
   "Transcode a EXAMPLE-BLOCK element from Org to HTML.
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (let* ((options (or (org-element-property :options example-block) ""))
-	 (value (org-export-handle-code example-block info)))
+	 (value (org-export-handle-code example-block info nil nil t)))
     ;; (org-e-html--wrap-label
     ;;  example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))
     (org-e-html--wrap-label
@@ -2590,8 +2592,14 @@ INFO is a plist holding contextual information.  See
      ;; Coderef: replace link with the reference name or the
      ;; equivalent line number.
      ((string= type "coderef")
-      (format (org-export-get-coderef-format path (or desc ""))
-	      (org-export-resolve-coderef path info)))
+      (let ((fragment (concat "coderef-" path)))
+	(format "<a href=#%s %s>%s</a>" fragment
+		(format (concat "class=\"coderef\""
+				" onmouseover=\"CodeHighlightOn(this, '%s');\""
+				" onmouseout=\"CodeHighlightOff(this, '%s');\"")
+			fragment fragment)
+		(format (org-export-get-coderef-format path (or desc "%s"))
+			(org-export-resolve-coderef path info)))))
      ;; Link type is handled by a special function.
      ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
       (funcall protocol (org-link-unescape path) desc 'html))
@@ -2808,7 +2816,7 @@ holding contextual information."
 CONTENTS holds the contents of the item.  INFO is a plist holding
 contextual information."
   (let* ((lang (org-element-property :language src-block))
-	 (code (org-export-handle-code src-block info))
+	 (code (org-export-handle-code src-block info nil nil t))
 	 (caption (org-element-property :caption src-block))
 	 (label (org-element-property :name src-block)))
     ;; FIXME: Handle caption

+ 43 - 37
EXPERIMENTAL/org-e-odt.el

@@ -566,7 +566,7 @@ styles congruent with the ODF-1.2 specification."
     (let ((xref-format "text"))
       (when (numberp desc)
 	(setq desc (format "%d" desc) xref-format "number"))
-      (org-e-odt-format-tags
+      (org-e-odt-format-tags-simple
        '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
 	 "</text:bookmark-ref>")
        desc xref-format href)))
@@ -575,7 +575,7 @@ styles congruent with the ODF-1.2 specification."
      '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
      desc href (or attr "")))
    (t
-    (org-e-odt-format-tags
+    (org-e-odt-format-tags-simple
      '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
      desc href (or attr "")))))
 
@@ -625,17 +625,18 @@ styles congruent with the ODF-1.2 specification."
 
 
 (defun org-e-odt-format-source-line-with-line-number-and-label
-  (line rpllbl num fontifier par-style)
-
-  (let ((keep-label (not (numberp rpllbl)))
-	(ref (org-find-text-property-in-string 'org-coderef line)))
-    (setq line (concat line (and keep-label ref (format "(%s)" ref))))
+  (line fontifier par-style)
+  (let (;; (keep-label (not (numberp rpllbl)))
+	(ref (org-find-text-property-in-string 'org-coderef line))
+	(num (org-find-text-property-in-string 'org-loc line)))
+    (setq line (concat line (and ref (format "(%s)" ref))))
     (setq line (funcall fontifier line))
     (when ref
       (setq line (org-e-odt-format-target line (concat "coderef-" ref))))
     (setq line (org-e-odt-format-stylized-paragraph par-style line))
     (if (not num) line
-      (org-e-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
+      (org-e-odt-format-tags
+       '("<text:list-item>" . "</text:list-item>") line))))
 
 (defun org-e-odt-format-source-code-or-example-plain
   (lines lang caption textareap cols rows num cont rpllbl fmt)
@@ -649,7 +650,7 @@ off."
      (lambda (line)
        (incf i)
        (org-e-odt-format-source-line-with-line-number-and-label
-	line rpllbl num 'org-e-odt-encode-plain-text
+	line 'org-e-odt-encode-plain-text
 	(if (= i line-count) "OrgFixedWidthBlockLastLine"
 	  "OrgFixedWidthBlock")))
      lines "\n")))
@@ -701,8 +702,7 @@ Update styles.xml with styles that were collected as part of
 	(goto-char (match-beginning 0))
 	(insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
 
-(defun org-e-odt-format-source-code-or-example-colored
-  (lines lang caption textareap cols rows num cont rpllbl fmt)
+(defun org-e-odt-format-source-code-or-example-colored (lines lang caption)
   "Format source or example blocks using `htmlfontify-string'.
 Use this routine when `org-export-e-odt-fontify-srcblocks' option
 is turned on."
@@ -740,37 +740,37 @@ is turned on."
 	 (lambda (line)
 	   (incf i)
 	   (org-e-odt-format-source-line-with-line-number-and-label
-	    line rpllbl num 'htmlfontify-string
+	    line 'htmlfontify-string
 	    (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
 	 lines "\n")))))
 
 (defun org-e-odt-format-source-code-or-example (lines lang
-						      &optional
-						      caption textareap
-						      cols rows num cont
-						      rpllbl fmt)
+						&optional caption ; FIXME
+						)
   "Format source or example blocks for export.
 Use `org-e-odt-format-source-code-or-example-plain' or
 `org-e-odt-format-source-code-or-example-colored' depending on the
 value of `org-export-e-odt-fontify-srcblocks."
   (setq ;; lines (org-export-number-lines
-	;;        lines 0 0 num cont rpllbl fmt 'preprocess) FIXME
-	lines (funcall
-	       (or (and org-export-e-odt-fontify-srcblocks
-			(or (featurep 'htmlfontify)
-			    ;; htmlfontify.el was introduced in Emacs 23.2
-			    ;; So load it with some caution
-			    (require 'htmlfontify nil t))
-			(fboundp 'htmlfontify-string)
-			'org-e-odt-format-source-code-or-example-colored)
-		   'org-e-odt-format-source-code-or-example-plain)
-	       lines lang caption textareap cols rows num cont rpllbl fmt))
-  (if (not num) lines
-    (let ((extra (format " text:continue-numbering=\"%s\""
-			 (if cont "true" "false"))))
-      (org-e-odt-format-tags
-       '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
-	 . "</text:list>") lines extra))))
+   ;;        lines 0 0 num cont rpllbl fmt 'preprocess) FIXME
+   lines (funcall
+	  (or (and org-export-e-odt-fontify-srcblocks
+		   (or (featurep 'htmlfontify)
+		       ;; htmlfontify.el was introduced in Emacs 23.2
+		       ;; So load it with some caution
+		       (require 'htmlfontify nil t))
+		   (fboundp 'htmlfontify-string)
+		   'org-e-odt-format-source-code-or-example-colored)
+	      'org-e-odt-format-source-code-or-example-plain)
+	  lines lang caption))
+  (let ((num (org-find-text-property-in-string 'org-loc lines)))
+    (if (not num) lines
+      (let* ((cont (not (equal num 1)))
+	     (extra (format " text:continue-numbering=\"%s\""
+			    (if cont "true" "false"))))
+	(org-e-odt-format-tags
+	 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
+	   . "</text:list>") lines extra)))))
 
 (defun org-e-odt-remap-stylenames (style-name)
   (or
@@ -1341,6 +1341,9 @@ ATTR is a string of other attributes of the a element."
 (defun org-e-odt-format-tags (tag text &rest args)
   (apply 'org-e-odt-format-tags-1 tag text "\n" "\n" args))
 
+(defun org-e-odt-format-tags-simple (tag text &rest args)
+  (apply 'org-e-odt-format-tags-1 tag text nil nil args))
+
 (defun org-e-odt-init-outfile ()
   (unless (executable-find "zip")
     ;; Not at all OSes ship with zip by default
@@ -3440,7 +3443,7 @@ contextual information."
   "Transcode a EXAMPLE-BLOCK element from Org to HTML.
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (let* ((options (or (org-element-property :options example-block) ""))
-	 (value (org-export-handle-code example-block info)))
+	 (value (org-export-handle-code example-block info nil nil t)))
     (org-e-odt--wrap-label
      example-block (org-e-odt-format-source-code-or-example value nil))))
 
@@ -3989,8 +3992,11 @@ INFO is a plist holding contextual information.  See
      ;; Coderef: replace link with the reference name or the
      ;; equivalent line number.
      ((string= type "coderef")
-      (format (org-export-get-coderef-format path (or desc ""))
-	      (org-export-resolve-coderef path info)))
+      (let* ((fmt (org-export-get-coderef-format path (or desc "%s")))
+	     (res (org-export-resolve-coderef path info))
+	     (org-e-odt-suppress-xref nil)
+	     (href (org-xml-format-href (concat "#coderef-" path))))
+	(format fmt (org-e-odt-format-link res href))))
      ;; Link type is handled by a special function.
      ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
       (funcall protocol (org-link-unescape path) desc 'html))
@@ -4178,7 +4184,7 @@ holding contextual information."
 CONTENTS holds the contents of the item.  INFO is a plist holding
 contextual information."
   (let* ((lang (org-element-property :language src-block))
-	 (code (org-export-handle-code src-block info))
+	 (code (org-export-handle-code src-block info nil nil t))
 	 (caption (org-element-property :caption src-block))
 	 (label (org-element-property :name src-block)))
     ;; FIXME: Handle caption