On 03/18/2010 04:36 AM, Jean-Pierre Chrétien wrote:
rgheck<rgh...@...>  writes:


Attached is the patch switching us from prettyref to refstyle. What
happens at the moment is pretty minimal, but there is more that could
now be done, e.g., allowing customization of the separator in labels,
which still defaults to the colon. See bug 6420.

Comments welcome.
I applied the patch and loaded an earlier document with a formatted reference, I
got this:

Warning: An error ocurred in 379,<function convert_prettyref at 0xb7bf279c>
Traceback (most recent call last):
   File "/ext/lyx-devel/lib/lyx2lyx/lyx2lyx", line 85, in<module>
     main()
   File "/ext/lyx-devel/lib/lyx2lyx/lyx2lyx", line 79, in main
     doc.convert()
   File "/ext/lyx-devel/lib/lyx2lyx/LyX.py", line 546, in convert
     conv(self)
   File "/ext/lyx-devel/lib/lyx2lyx/lyx_2_0.py", line 627, in convert_prettyref
     m = re_ref.match(document.body[k])
NameError: global name 're_ref' is not defined
Error: Conversion script failed

Sorry. I'm not sure how the declaration of re_ref got commented out in the patch I posted. Here's a corrected one.

I have a further comment about the lyx2lyx conversion: prettyref knows about
theorems and lemmas, refstyle does not. What shoud be done to deal with this ?
refstyle.cfg can be upgraded of course, but how should lyx publish it ?

I was going to raise this myself. One possibility is to include the \newref commands in the Preamble section of the layout. Either we would then have to load refstyle no matter what, or else we'd have to wrap it in a check for whether it is loaded, probably the latter. Alternatively, we could have a new layout command, RefCommand or whatever, that would declare those commands:
    RefPreamble
        \newref{thm}{%
            name = \RSthmtxt,
            names = \RSthmstxt,
        % etc
        }
        \def\RSthmtxt{theorem~}
        % etc
    EndPreamble
And then this would be included whenever refstyle is loaded. (A more subtle check could be done, but perhaps isn't necessary.)

The same question applies for i18n as well (a few languages are currently known
by refstyle.cfg).

Here, I am less sure how much we can do. People who need languages refstyle does not know can add the relevant commands to a module, or else use a customized version of refstyle.cfg. By using refstyle, we give people this kind of option, whereas they did not have it before. But we don't and can't claim to do the i18n for every language LyX supports. That said, we could encourage our translators to send translations to the refstyle maintainer for inclusion in refstyle.cfg.

rh


Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revision 33790)
+++ src/Buffer.cpp	(working copy)
@@ -126,7 +126,7 @@
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 378; // ps: rev insetinfo
+int const LYX_FORMAT = 379; // rgh: refstyle
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
Index: src/insets/InsetRef.cpp
===================================================================
--- src/insets/InsetRef.cpp	(revision 33790)
+++ src/insets/InsetRef.cpp	(working copy)
@@ -24,6 +24,7 @@
 #include "sgml.h"
 #include "TocBackend.h"
 
+#include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
@@ -51,7 +52,7 @@
 		|| s == "pageref"
 		|| s == "vref" 
 		|| s == "vpageref"
-		|| s == "prettyref"
+		|| s == "refstyle"
 		|| s == "eqref";
 }
 
@@ -74,6 +75,25 @@
 	// in docbook. So we construct new params, without it, and use that.
 	InsetCommandParams p(REF_CODE, getCmdName());
 	p["reference"] = getParam("reference");
+	string const cmd = p.getCmdName();
+	if (cmd != "refstyle") {
+		os << p.getCommand(runparams);
+		return 0;
+	}
+	// refstyle isn't really a command. rather, it signals that
+	// we need to create the command needed.
+	docstring prefix;
+	docstring const rest = split(p["reference"], prefix, ':');
+	// if rest is empty, then there's no ":", so we'll treat
+	// it simply as a normal reference.
+	if (rest.empty()) {
+		LYXERR0("Couldn't determine reference type for label `" << 
+				prefix << "'");
+		p.setCmdName("ref");
+	} else {
+		p.setCmdName(to_utf8(prefix) + "ref");
+		p["reference"] = rest;
+	}
 	os << p.getCommand(runparams);
 	return 0;
 }
@@ -208,8 +228,8 @@
 {
 	if (getCmdName() == "vref" || getCmdName() == "vpageref")
 		features.require("varioref");
-	else if (getCmdName() == "prettyref")
-		features.require("prettyref");
+	else if (getCmdName() == "refstyle")
+		features.require("refstyle");
 	else if (getCmdName() == "eqref")
 		features.require("amsmath");
 }
@@ -221,7 +241,7 @@
 	{ "pageref",   N_("Page Number"),           N_("Page: ")},
 	{ "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
 	{ "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
-	{ "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
+	{ "refstyle",  N_("RefStyle"),              N_("RefStyle: ")},
 	{ "", "", "" }
 };
 
Index: src/LaTeXFeatures.cpp
===================================================================
--- src/LaTeXFeatures.cpp	(revision 33790)
+++ src/LaTeXFeatures.cpp	(working copy)
@@ -510,7 +510,7 @@
 	"pifont",
 	// subfig is handled in BufferParams.cpp
 	"varioref",
-	"prettyref",
+	"refstyle",
 	/*For a successful cooperation of the `wrapfig' package with the
 	  `float' package you should load the `wrapfig' package *after*
 	  the `float' package. See the caption package documentation
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp	(revision 33790)
+++ src/LyXAction.cpp	(working copy)
@@ -2305,7 +2305,7 @@
                pageref -- <page> \n
                vpageref -- on <page> \n
                vref -- <reference> on <page> \n
-               prettyref -- Formatted reference
+               refstyle -- Formatted reference
  * \endvar
  */
 		{ LFUN_INSET_INSERT, "inset-insert", Noop, Edit },
Index: lib/lyx2lyx/lyx_2_0.py
===================================================================
--- lib/lyx2lyx/lyx_2_0.py	(revision 33790)
+++ lib/lyx2lyx/lyx_2_0.py	(working copy)
@@ -602,6 +602,41 @@
         i = i + 1
 
 
+def convert_prettyref(document):
+    " Converts prettyref commands to refstyle commands "
+    re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Malformed LyX document: No end of InsetRef")
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand prettyref", i)
+        if k == -1 or k > j:
+            i = j + 1
+            continue
+        document.body[k] = "LatexCommand refstyle"
+        k = find_token(document.body, "reference", i)
+        if k == -1 or k > j:
+            i = j + 1
+            continue
+        m = re_ref.match(document.body[k])
+        if m:
+            # Note: We could switch from ":" to something else here, though
+            # that would mean doing a lot of \newref somewhere.
+            prefix = m.group(1)
+            suffix = m.group(2)
+            if prefix == "cha":
+                prefix = "chap"
+            elif prefix == "par":
+                prefix == "part"
+            document.body[k] = prefix + ":" + suffix
+        i = j + 1
+
 def convert_splitindex(document):
     " Converts index and printindex insets to splitindex-aware format "
     i = 0
@@ -1242,10 +1277,12 @@
            [375, []],
            [376, []],
            [377, []],
-           [378, []]
+           [378, []],
+           [379, [convert_prettyref]]
           ]
 
-revert =  [[377, []],
+revert =  [#[378, [revert_refstyle]]
+           [377, []],
            [376, [revert_multirow]],
            [375, [revert_includeall]],
            [374, [revert_includeonly]],
Index: lib/symbols
===================================================================
--- lib/symbols	(revision 33790)
+++ lib/symbols	(working copy)
@@ -131,7 +131,7 @@
 
 # references
 pageref           ref         none
-prettyref         ref         none
+refstyle          ref         none
 ref               ref         none
 vpageref          ref         none
 vref              ref         none
Index: lib/ui/stdcontext.inc
===================================================================
--- lib/ui/stdcontext.inc	(revision 33790)
+++ lib/ui/stdcontext.inc	(working copy)
@@ -90,7 +90,7 @@
 		Item "<Page>|P" "inset-modify changetype pageref"
 		Item "On Page <Page>|O" "inset-modify changetype vpageref"
 		Item "<Reference> on Page <Page>|f" "inset-modify changetype vref"
-		Item "Formatted Reference|t" "inset-modify changetype prettyref"
+		Item "Formatted Reference|t" "inset-modify changetype refstyle"
 		Separator
 		Item "Settings...|S" "inset-settings"
 	End
Index: lib/chkconfig.ltx
===================================================================
--- lib/chkconfig.ltx	(revision 33790)
+++ lib/chkconfig.ltx	(working copy)
@@ -265,8 +265,8 @@
 \TestPackage{nomencl}
 \TestPackage{pdfcolmk}
 \TestPackage{pdfpages}
-\TestPackage{prettyref}
 \TestPackage{preview}
+\TestPackage{refstyle}
 \TestPackage{rotating}
 \TestPackage{rotfloat}
 \TestPackage{setspace}

Reply via email to