Просмотр исходного кода

org-odt.el: Add support for native numbering of src and example lines

* contrib/lisp/org-lparse.el (org-lparse-format-org-link):
Push the responsibility of calling
`org-export-get-coderef-format' to the backends.

* contrib/lisp/org-xhtml.el (org-xhtml-format-org-link): Take
up the above responsibility.

* contrib/lisp/org-odt.el (org-odt-format-link): Handle links whose
descriptions are actually line-numbers.
(org-odt-format-source-line-with-line-number-and-label): New.
Generate line numbers and link targets for lines that are part of
source or example blocks.
(org-odt-format-source-code-or-example-plain)
(org-odt-format-source-code-or-example-colored): Use
`org-odt-format-source-line-with-line-number-and-label'.
(org-odt-format-source-code-or-example): Pre-process input lines with
`org-export-number-lines'.  Also handle numbering of src lines.
(org-odt-format-org-link): Rework handling of coderef links.  Generate
ODF-specific markup for line-number based coderef links.
(org-xml-encode-plain-text-lines): Removed.  Not used any more.

contrib/odt/styles/OrgOdtStyles.xml (OrgSrcBlockNumberedLine): New
style for automatic numbering of src and example lines.
Jambunathan K 13 лет назад
Родитель
Сommit
8aedd359bb
4 измененных файлов с 114 добавлено и 39 удалено
  1. 2 8
      contrib/lisp/org-lparse.el
  2. 71 26
      contrib/lisp/org-odt.el
  3. 8 5
      contrib/lisp/org-xhtml.el
  4. 33 0
      contrib/odt/styles/OrgOdtStyles.xml

+ 2 - 8
contrib/lisp/org-lparse.el

@@ -254,14 +254,8 @@ OPT-PLIST is the export options list."
 	       'ORG-LINK opt-plist type path nil desc attr descp)))
 
        ((string= type "coderef")
-	(setq rpl
-	      (org-lparse-format
-	       'ORG-LINK opt-plist type "" (format "coderef-%s" path)
-	       (format
-		(org-export-get-coderef-format
-		 path
-		 (and descp desc))
-		(cdr (assoc path org-export-code-refs))) nil descp)))
+	(setq rpl (org-lparse-format
+		   'ORG-LINK opt-plist type "" path desc nil descp)))
 
        ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
 	;; The link protocol has a function for format the link

+ 71 - 26
contrib/lisp/org-odt.el

@@ -917,10 +917,13 @@ styles congruent with the ODF-1.2 specification."
   (cond
    ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
     (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
-    (org-odt-format-tags
-     '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
-       "</text:bookmark-ref>")
-     desc href))
+    (let ((xref-format "text"))
+      (when (numberp desc)
+	(setq desc (format "%d" desc) xref-format "number"))
+      (org-odt-format-tags
+       '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
+	 "</text:bookmark-ref>")
+       desc xref-format href)))
    (org-lparse-link-description-is-image
     (org-odt-format-tags
      '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
@@ -978,18 +981,32 @@ to make available an enhanced version of `htmlfontify' library."
   :type 'boolean
   :group 'org-export-odt)
 
+(defun org-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))))
+    (setq line (funcall fontifier line))
+    (when ref
+      (setq line (org-odt-format-target line (concat "coderef-" ref))))
+    (setq line (org-odt-format-stylized-paragraph par-style line))
+    (when num
+      (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
+
 (defun org-odt-format-source-code-or-example-plain
   (lines lang caption textareap cols rows num cont rpllbl fmt)
   "Format source or example blocks much like fixedwidth blocks.
 Use this when `org-export-odt-use-htmlfontify' option is turned
 off."
-  (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
-				       0 0 num cont rpllbl fmt))
-    (mapconcat
-     (lambda (line)
-       (org-odt-format-stylized-paragraph
-	'fixedwidth (org-odt-fill-tabs-and-spaces line)))
-     (org-split-string lines "[\r\n]") "\n"))
+  (mapconcat
+   (lambda (line)
+     (org-odt-format-source-line-with-line-number-and-label
+      line rpllbl num (lambda (line)
+			(org-odt-fill-tabs-and-spaces
+			 (org-xml-encode-plain-text line)))
+      'fixedwidth))
+   (org-split-string lines "[\r\n]") "\n"))
 
 (defvar org-src-block-paragraph-format
   "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
@@ -1102,7 +1119,8 @@ turned on."
     (when (fboundp 'htmlfontify-string)
       (mapconcat
        (lambda (line)
-	 (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
+	 (org-odt-format-source-line-with-line-number-and-label
+	  line rpllbl num 'htmlfontify-string 'src))
        (org-split-string lines "[\r\n]") "\n"))))
 
 (defun org-odt-format-source-code-or-example (lines lang caption textareap
@@ -1112,16 +1130,22 @@ turned on."
 Use `org-odt-format-source-code-or-example-plain' or
 `org-odt-format-source-code-or-example-colored' depending on the
 value of `org-export-odt-use-htmlfontify."
-  (funcall
-   (if (and org-export-odt-use-htmlfontify
-	    (or (featurep 'htmlfontify) (require 'htmlfontify))
-	    (fboundp 'htmlfontify-string))
-       'org-odt-format-source-code-or-example-colored
-     'org-odt-format-source-code-or-example-plain)
-   lines lang caption textareap cols rows num cont rpllbl fmt))
-
-(defun org-xml-encode-plain-text-lines (rtn)
-  (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
+  (setq lines (org-export-number-lines
+	       lines 0 0 num cont rpllbl fmt 'preprocess)
+	lines (funcall
+	       (or (and org-export-odt-use-htmlfontify
+			(or (featurep 'htmlfontify)
+			    (require 'htmlfontify))
+			(fboundp 'htmlfontify-string)
+			'org-odt-format-source-code-or-example-colored)
+		   'org-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-odt-format-tags
+       '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
+	 . "</text:list>") lines extra))))
 
 (defun org-odt-remap-stylenames (style-name)
   (or
@@ -1265,6 +1289,28 @@ MAY-INLINE-P allows inlining it as an image."
 	     (org-odt-is-formula-link-p filename)
 	     (or (not descp)))
 	(org-odt-format-inline-formula thefile))
+       ((string= type "coderef")
+	(let* ((ref fragment)
+	       (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
+	       (desc (and descp desc))
+	       (org-odt-suppress-xref nil)
+	       (href (org-xml-format-href (concat "#coderef-" ref))))
+	  (cond
+	   ((and (numberp lineno-or-ref) (not desc))
+	    (org-odt-format-link lineno-or-ref href))
+	   ((and (numberp lineno-or-ref) desc
+		 (string-match (regexp-quote (concat "(" ref ")")) desc))
+	    (format (replace-match "%s" t t desc)
+		    (org-odt-format-link lineno-or-ref href)))
+	   (t
+	    (setq desc (format
+			(if (and desc (string-match
+				       (regexp-quote (concat "(" ref ")"))
+				       desc))
+			    (replace-match "%s" t t desc)
+			  (or desc "%s"))
+			lineno-or-ref))
+	    (org-odt-format-link (org-xml-format-desc desc) href)))))
        (t
 	(when (string= type "file")
 	  (setq thefile
@@ -1274,16 +1320,15 @@ MAY-INLINE-P allows inlining it as an image."
 		 (t (org-odt-relocate-relative-path
 		     thefile org-current-export-file)))))
 
-	(when (and (member type '("" "http" "https" "file" "coderef"))
-		   fragment)
+	(when (and (member type '("" "http" "https" "file")) fragment)
 	  (setq thefile (concat thefile "#" fragment)))
 
 	(setq thefile (org-xml-format-href thefile))
 
-	(when (not (member type '("" "file" "coderef")))
+	(when (not (member type '("" "file")))
 	  (setq thefile (concat type ":" thefile)))
 
-	(let ((org-odt-suppress-xref (string= type "coderef")))
+	(let ((org-odt-suppress-xref nil))
 	  (org-odt-format-link
 	   (org-xml-format-desc desc) thefile attr)))))))
 

+ 8 - 5
contrib/lisp/org-xhtml.el

@@ -715,7 +715,7 @@ See variable `org-export-xhtml-link-org-files-as-html'"
 
 ;;; org-xhtml-format-org-link
 (defun org-xhtml-format-org-link (opt-plist type-1 path fragment desc attr
-					   descp)
+					    descp)
   "Make an HTML link.
 OPT-PLIST is an options list.
 TYPE is the device-type of the link (THIS://foo.html)
@@ -725,11 +725,14 @@ DESC is the link description, if any.
 ATTR is a string of other attributes of the a element.
 MAY-INLINE-P allows inlining it as an image."
   (declare (special org-lparse-par-open))
-  (when (string= type-1 "coderef")
-    (setq attr
-	  (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
-		  fragment fragment)))
   (save-match-data
+    (when (string= type-1 "coderef")
+      (let ((ref fragment))
+	(setq desc (format (org-export-get-coderef-format ref (and descp desc))
+			   (cdr (assoc ref org-export-code-refs)))
+	      fragment (concat  "coderef-" ref)
+	      attr (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
+			   fragment fragment))))
     (let* ((may-inline-p
 	    (and (member type-1 '("http" "https" "file"))
 		 (org-lparse-should-inline-p path descp)

+ 33 - 0
contrib/odt/styles/OrgOdtStyles.xml

@@ -643,6 +643,39 @@
    </text:list-level-style-number>
   </text:list-style>
 
+  <text:list-style style:name="OrgSrcBlockNumberedLine">
+   <text:list-level-style-number text:level="1" style:num-format="1">
+    <style:list-level-properties text:space-before="0.635cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="2" style:num-format="1">
+    <style:list-level-properties text:space-before="1.27cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="3" style:num-format="1">
+    <style:list-level-properties text:space-before="1.905cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="4" style:num-format="1">
+    <style:list-level-properties text:space-before="2.54cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="5" style:num-format="1">
+    <style:list-level-properties text:space-before="3.175cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="6" style:num-format="1">
+    <style:list-level-properties text:space-before="3.81cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="7" style:num-format="1">
+    <style:list-level-properties text:space-before="4.445cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="8" style:num-format="1">
+    <style:list-level-properties text:space-before="5.08cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="9" style:num-format="1">
+    <style:list-level-properties text:space-before="5.715cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+   <text:list-level-style-number text:level="10" style:num-format="1">
+    <style:list-level-properties text:space-before="6.35cm" text:min-label-width="0.635cm" text:min-label-distance="0.101cm" fo:text-align="end"/>
+   </text:list-level-style-number>
+  </text:list-style>
+
   <text:notes-configuration text:note-class="footnote" text:citation-style-name="Footnote_20_Symbol" text:citation-body-style-name="Footnote_20_anchor" style:num-format="1" text:start-value="0" text:footnotes-position="page" text:start-numbering-at="document"/>
   <text:notes-configuration text:note-class="endnote" style:num-format="i" text:start-value="0"/>
   <text:linenumbering-configuration text:number-lines="false" text:offset="0.499cm" style:num-format="1" text:number-position="left" text:increment="5"/>