Add ability to use refstyle's plural and capitalization features.

This commit is contained in:
Richard Heck 2016-06-18 19:29:15 -04:00
parent 78a76cc8d4
commit 73f59e87bd
8 changed files with 181 additions and 72 deletions

View File

@ -27,9 +27,9 @@ import sys, os
from parser_tools import find_end_of, find_token_backwards, find_end_of_layout, \
find_token, find_end_of_inset, get_value, get_bool_value, \
get_containing_layout
# find_tokens, find_token_exact, is_in_inset, get_quoted_value, \
# del_token, check_token, get_option_value
get_containing_layout, get_quoted_value, del_token
# find_tokens, find_token_exact, is_in_inset, \
# check_token, get_option_value
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
# get_ert, lyx2latex, \
@ -1076,23 +1076,58 @@ def revert_labelonly(document):
if k == -1:
i = j
continue
m = find_token(document.body, "reference", i, j)
if m == -1:
label = get_quoted_value(document.body, "reference", i, j)
if not label:
document.warning("Can't find label for reference at line %d!" %(i))
i = j + 1
continue
lm = re.match(r'reference\s+"([^"]+)"', document.body[m])
if not lm:
document.warning("Can't find label for reference at line %d!" %(i))
i = j + 1
continue
label = lm.group(1)
document.body[i:j+1] = put_cmd_in_ert([label])
i = j + 1
continue
def revert_plural_refs(document):
" Revert plural and capitalized references "
i = find_token(document.header, "\\use_refstyle 1", 0)
use_refstyle = (i != 0)
i = 0
while (True):
i = find_token(document.body, "\\begin_inset CommandInset ref", i)
if i == -1:
return
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Can't find end of reference inset at line %d!!" %(i))
i += 1
continue
plural = caps = label = False
if use_refstyle:
plural = get_bool_value(document.body, "plural", i, j, False)
caps = get_bool_value(document.body, "caps", i, j, False)
label = get_quoted_value(document.body, "reference", i, j)
if label:
(prefix, suffix) = label.split(":", 1)
else:
document.warning("Can't find label for reference at line %d!" % (i))
# this effectively tests also for use_refstyle
if not ((plural or caps) and label):
del_token(document.body, "plural", i, j)
del_token(document.body, "caps", i, j - 1) # since we deleted a line
i = j - 1
continue
if caps:
prefix = prefix[0].title() + prefix[1:]
cmd = "\\" + prefix + "ref"
if plural:
cmd += "[s]"
cmd += "{" + suffix + "}"
document.body[i:j+1] = put_cmd_in_ert([cmd])
i = j + 1
##
# Conversion hub
#
@ -1115,10 +1150,12 @@ convert = [
[522, []],
[523, []],
[524, []],
[525, []]
[525, []],
[526, []]
]
revert = [
[525, [revert_plural_refs]],
[524, [revert_labelonly]],
[523, [revert_crimson, revert_cochinealmath]],
[522, [revert_cjkquotes]],

View File

@ -64,7 +64,7 @@ get_value(lines, token, start[, end[, default]):
and is what is returned if we do not find anything. So you
can use that to set a default.
get_quoted_value(lines, token, start[, end[, default]):
get_quoted_value(lines, token, start[, end[, default]]):
Similar to get_value, but it will strip quotes off the
value, if they are present. So use this one for cases
where the value is normally quoted.
@ -74,6 +74,9 @@ get_option_value(line, option):
option="value"
and returns value. Returns "" if not found.
get_bool_value(lines, token, start[, end[, default]]):
Like get_value, but returns a boolean.
del_token(lines, token, start[, end]):
Like find_token, but deletes the line if it finds one.
Returns True if a line got deleted, otherwise False.
@ -325,7 +328,7 @@ def get_bool_value(lines, token, start, end = 0, default = None):
False if bool_value is 0 or false
"""
val = get_value(lines, token, start, end, "")
val = get_quoted_value(lines, token, start, end, "")
if val == "1" or val == "true":
return True

View File

@ -16,6 +16,7 @@
#include "GuiApplication.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferList.h"
#include "FuncRequest.h"
@ -105,6 +106,12 @@ GuiRef::GuiRef(GuiView & lv)
this, SLOT(updateClicked()));
connect(bufferCO, SIGNAL(activated(int)),
this, SLOT(updateClicked()));
connect(pluralCB, SIGNAL(clicked()),
this, SLOT(changed_adaptor()));
connect(capsCB, SIGNAL(clicked()),
this, SLOT(changed_adaptor()));
enableBoxes();
bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
bc().setOK(okPB);
@ -126,9 +133,20 @@ void GuiRef::enableView(bool enable)
}
void GuiRef::enableBoxes()
{
bool const isFormatted =
(InsetRef::getName(typeCO->currentIndex()) == "formatted");
bool const usingRefStyle = buffer().params().use_refstyle;
pluralCB->setEnabled(isFormatted && usingRefStyle);
capsCB->setEnabled (isFormatted && usingRefStyle);
}
void GuiRef::changed_adaptor()
{
changed();
enableBoxes();
}
@ -281,6 +299,9 @@ void GuiRef::updateContents()
if (!typeAllowed())
typeCO->setCurrentIndex(0);
pluralCB->setChecked(params_["plural"] == "true");
capsCB->setChecked(params_["caps"] == "true");
// insert buffer list
bufferCO->clear();
FileNameList const buffers(theBufferList().fileNames());
@ -303,6 +324,7 @@ void GuiRef::updateContents()
active_buffer_ = thebuffer;
updateRefs();
enableBoxes();
// Activate OK/Apply buttons if the users inserts a new ref
// and we have a valid pre-setting.
bc().setValid(isValid() && new_inset);
@ -316,7 +338,10 @@ void GuiRef::applyView()
params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
params_["reference"] = qstring_to_ucs4(last_reference_);
params_["name"] = qstring_to_ucs4(nameED->text());
params_["plural"] = pluralCB->isChecked() ?
from_ascii("true") : from_ascii("false");
params_["caps"] = capsCB->isChecked() ?
from_ascii("true") : from_ascii("false");
restored_buffer_ = bufferCO->currentIndex();
}

View File

@ -69,6 +69,8 @@ private:
void applyView();
/// update dialog
void updateContents();
///
void enableBoxes();
/// is name allowed for this ?
bool nameAllowed();

View File

@ -17,6 +17,42 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
<widget class="QTreeWidget" name="refsTW">
<column>
<property name="text">
<string>1</string>
</property>
</column>
</widget>
</item>
<item row="7" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="nameL">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>nameED</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="nameED">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
@ -79,25 +115,6 @@
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Available &amp;Labels:</string>
</property>
<property name="buddy">
<cstring>refsTW</cstring>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QTreeWidget" name="refsTW">
<column>
<property name="text">
<string>1</string>
</property>
</column>
</widget>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
@ -157,6 +174,16 @@
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Available &amp;Labels:</string>
</property>
<property name="buddy">
<cstring>refsTW</cstring>
</property>
</widget>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
@ -267,34 +294,7 @@
</item>
</layout>
</item>
<item row="6" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="nameL">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>nameED</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="nameED">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="7" column="0">
<item row="8" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
@ -381,6 +381,24 @@
</item>
</layout>
</item>
<item row="6" column="0">
<layout class="QHBoxLayout" name="horizontalLayout9">
<item>
<widget class="QCheckBox" name="pluralCB">
<property name="text">
<string>Plural</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="capsCB">
<property name="text">
<string>Capitalized</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>

View File

@ -69,11 +69,23 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
ParamInfo::HANDLING_ESCAPE);
param_info_.add("plural", ParamInfo::LYX_INTERNAL);
param_info_.add("caps", ParamInfo::LYX_INTERNAL);
}
return param_info_;
}
namespace {
void capitalize(docstring & s) {
char_type t = uppercase(s[0]);
s[0] = t;
}
}
// the ref argument is the label name we are referencing.
// we expect ref to be in the form: pfx:suffix.
//
@ -89,7 +101,7 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
// label, thus: \prettyref{pfx:suffix}.
//
docstring InsetRef::getFormattedCmd(docstring const & ref,
docstring & label, docstring & prefix) const
docstring & label, docstring & prefix, docstring const & caps) const
{
static docstring const defcmd = from_ascii("\\ref");
static docstring const prtcmd = from_ascii("\\prettyref");
@ -127,6 +139,9 @@ docstring InsetRef::getFormattedCmd(docstring const & ref,
return defcmd;
}
}
if (caps == "true") {
capitalize(prefix);
}
return from_ascii("\\") + prefix + from_ascii("ref");
}
@ -158,8 +173,12 @@ void InsetRef::latex(otexstream & os, OutputParams const & rp) const
else if (cmd == "formatted") {
docstring label;
docstring prefix;
docstring const fcmd = getFormattedCmd(data, label, prefix);
os << fcmd << '{' << label << '}';
docstring const fcmd =
getFormattedCmd(data, label, prefix, getParam("caps"));
os << fcmd;
if (buffer().params().use_refstyle && getParam("plural") == "true")
os << "[s]";
os << '{' << label << '}';
}
else if (cmd == "labelonly") {
os << getParam("reference");
@ -233,8 +252,12 @@ docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
op.local_font->language()->lang());
else if (cmd == "eqref")
display_string = '(' + value + ')';
else if (cmd == "formatted")
else if (cmd == "formatted") {
display_string = il->prettyCounter();
if (buffer().params().use_refstyle && getParam("caps") == "true")
capitalize(display_string);
// it is hard to see what to do about plurals...
}
else if (cmd == "nameref")
// FIXME We don't really have the ability to handle these
// properly in XHTML output yet (bug #8599).
@ -327,7 +350,8 @@ void InsetRef::validate(LaTeXFeatures & features) const
docstring const data = getEscapedLabel(features.runparams());
docstring label;
docstring prefix;
docstring const fcmd = getFormattedCmd(data, label, prefix);
docstring const fcmd =
getFormattedCmd(data, label, prefix, getParam("caps"));
if (buffer().params().use_refstyle) {
features.require("refstyle");
if (prefix == "cha")

View File

@ -111,7 +111,7 @@ private:
/// \param argument for reference command
/// \param prefix of the label (before :)
docstring getFormattedCmd(docstring const & ref, docstring & label,
docstring & prefix) const;
docstring & prefix, docstring const & caps) const;
///
mutable docstring screen_label_;

View File

@ -32,7 +32,7 @@ extern char const * const lyx_version_info;
// Do not remove the comment below, so we get merge conflict in
// independent branches. Instead add your own.
#define LYX_FORMAT_LYX 525 // rgh: labelonly for references
#define LYX_FORMAT_LYX 526 // rgh: labelonly for references
#define LYX_FORMAT_TEX2LYX 525
#if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX