From: Daiki Ueno Date: Fri, 25 Sep 2015 22:28:15 +0000 (+0900) Subject: its: More refactoring X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=458c994e0cab3f1544c1236cd5f79a97d339664b;p=thirdparty%2Fgettext.git its: More refactoring --- diff --git a/gettext-tools/src/its.c b/gettext-tools/src/its.c index b491db88c..4ff182520 100644 --- a/gettext-tools/src/its.c +++ b/gettext-tools/src/its.c @@ -48,15 +48,17 @@ extending the its_rule_class_ty class and registering it in init_classes(). - The message extraction is done in three phases. In the first - phase, its_rule_list_apply() associates values to matching XML - elements in the target document. In the second phase, it traverses - the tree and marks translatable elements. In the final phase, it - extracts text contents from the marked elements. - - The values associated with an XML node are represented as an array - of key-value pairs, where both keys and values are string. The - values are stored in node->_private. */ + The message extraction is performed in three steps. In the first + step, its_rule_list_apply() assigns values to nodes in an XML + document. In the second step, its_rule_list_extract_nodes() marks + translatable nodes. In the final step, + its_rule_list_extract_text() extracts text contents from the marked + nodes. + + The values assigned to a node are represented as an array of + key-value pairs, where both keys and values are string. The array + is stored in node->_private. To retrieve the values for a node, + use its_rule_list_eval(). */ #define ITS_NS "http://www.w3.org/2005/11/its" @@ -366,42 +368,37 @@ _its_get_attribute (xmlNode *node, const char *attr) return result; } -enum whitespace_type_ty +enum its_whitespace_type_ty { - none, - normalize, - strip + preserve, + normalize }; -typedef enum whitespace_type_ty whitespace_type_ty; static char * -normalize_whitespace (const char *text, whitespace_type_ty whitespace) +normalize_whitespace (const char *text, enum its_whitespace_type_ty whitespace) { - if (whitespace == none) + if (whitespace == preserve) return xstrdup (text); else + /* Normalize whitespaces within the text, but not at the beginning + nor the end of the text. */ { - char *result, *p; + char *result, *p, *end; result = xstrdup (text); - - /* Normalize whitespaces within the text. */ - if (whitespace == normalize) + end = result + strlen (result); + for (p = result; *p != '\0';) { - char *end = result + strlen (result); - for (p = result; *p != '\0';) + size_t len = strspn (p, " \t\n"); + if (len > 0) { - size_t len = strspn (p, " \t\n"); - if (len > 0) - { - *p = ' '; - memmove (p + 1, p + len, end - (p + len)); - end -= len - 1; - *end = '\0'; - p++; - } - p += strcspn (p, " \t\n"); + *p = ' '; + memmove (p + 1, p + len, end - (p + len)); + end -= len - 1; + *end = '\0'; + p++; } + p += strcspn (p, " \t\n"); } return result; } @@ -471,7 +468,8 @@ _its_encode_special_chars (const char *content, bool is_attribute) } static char * -_its_collect_text_content (xmlNode *node, whitespace_type_ty whitespace) +_its_collect_text_content (xmlNode *node, + enum its_whitespace_type_ty whitespace) { char *buffer = NULL; size_t bufmax = 0; @@ -640,7 +638,6 @@ its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool, xmlNode *node) { struct its_value_list_ty *result; - xmlNode *n = node; result = XCALLOC (1, struct its_value_list_ty); @@ -676,6 +673,8 @@ its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool, case XML_ELEMENT_NODE: /* Inherit from the parent elements. */ { + xmlNode *n; + for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent) { const char *value = @@ -761,7 +760,6 @@ its_localization_note_rule_eval (struct its_rule_ty *pop, xmlNode *node) { struct its_value_list_ty *result; - xmlNode *n = node; result = XCALLOC (1, struct its_value_list_ty); @@ -788,24 +786,57 @@ its_localization_note_rule_eval (struct its_rule_ty *pop, return result; } - /* Inherit from the parent elements. */ - for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent) + switch (node->type) { - const char *value; + case XML_ATTRIBUTE_NODE: + /* Attribute nodes don't inherit from the parent elements. */ + { + const char *value; - value = _its_pool_get_value_for_node (pool, n, "locNote"); - if (value != NULL) - { - its_value_list_set_value (result, "locNote", value); - return result; - } + value = _its_pool_get_value_for_node (pool, node, "locNote"); + if (value != NULL) + { + its_value_list_set_value (result, "locNote", value); + return result; + } - value = _its_pool_get_value_for_node (pool, n, "locNotePointer"); - if (value != NULL) - { - its_value_list_set_value (result, "locNotePointer", value); - return result; - } + value = _its_pool_get_value_for_node (pool, node, "locNotePointer"); + if (value != NULL) + { + its_value_list_set_value (result, "locNotePointer", value); + return result; + } + } + break; + + case XML_ELEMENT_NODE: + /* Inherit from the parent elements. */ + { + xmlNode *n; + + for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent) + { + const char *value; + + value = _its_pool_get_value_for_node (pool, n, "locNote"); + if (value != NULL) + { + its_value_list_set_value (result, "locNote", value); + return result; + } + + value = _its_pool_get_value_for_node (pool, n, "locNotePointer"); + if (value != NULL) + { + its_value_list_set_value (result, "locNotePointer", value); + return result; + } + } + } + break; + + default: + break; } /* The default value is None. */ @@ -922,7 +953,7 @@ its_preserve_space_rule_eval (struct its_rule_ty *pop, xmlNode *node) { struct its_value_list_ty *result; - const char *value; + xmlNode *n; result = XCALLOC (1, struct its_value_list_ty); @@ -937,13 +968,15 @@ its_preserve_space_rule_eval (struct its_rule_ty *pop, return result; } - /* Doesn't inherit from the parent elements, and the default value - is None. */ - value = _its_pool_get_value_for_node (pool, node, "space"); - if (value != NULL) + /* Inherit from the parent elements. */ + for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent) { - its_value_list_set_value (result, "space", value); - return result; + const char *value = _its_pool_get_value_for_node (pool, n, "space"); + if (value != NULL) + { + its_value_list_set_value (result, "space", value); + return result; + } } /* The default value is space="default". */ @@ -1309,7 +1342,7 @@ its_rule_list_extract_text (its_rule_list_ty *rules, const char *value; char *content; char *comment = NULL; - whitespace_type_ty whitespace; + enum its_whitespace_type_ty whitespace; values = its_rule_list_eval (rules, node); @@ -1325,7 +1358,7 @@ its_rule_list_extract_text (its_rule_list_ty *rules, value = its_value_list_get_value (values, "space"); if (value && strcmp (value, "preserve") == 0) - whitespace = none; + whitespace = preserve; else whitespace = normalize; @@ -1348,7 +1381,7 @@ its_rule_list_extract_text (its_rule_list_ty *rules, content, null_context, &pos, comment, NULL); - if (whitespace == none) + if (whitespace == preserve) message->do_wrap = no; if (node->type == XML_ELEMENT_NODE) diff --git a/gettext-tools/src/xlocator.c b/gettext-tools/src/xlocator.c index 35d305901..a30b7d89e 100644 --- a/gettext-tools/src/xlocator.c +++ b/gettext-tools/src/xlocator.c @@ -32,6 +32,7 @@ # define HAVE_DIR 0 #endif +#include "basename.h" #include #include "error.h" #include @@ -119,7 +120,8 @@ xlocator_match (struct xlocator_ty *locator, const char *path, case XLOCATOR_URI_PATTERN: /* FIXME: We should not use fnmatch() here, since PATTERN is a URI, with a wildcard. */ - return fnmatch (locator->matcher.pattern, path, 0) == 0; + return fnmatch (locator->matcher.pattern, basename (path), FNM_PATHNAME) + == 0; case XLOCATOR_NAMESPACE: case XLOCATOR_DOCUMENT_ELEMENT: diff --git a/gettext-tools/tests/xgettext-its-1 b/gettext-tools/tests/xgettext-its-1 index 878bc75c7..d1359ae98 100755 --- a/gettext-tools/tests/xgettext-its-1 +++ b/gettext-tools/tests/xgettext-its-1 @@ -57,8 +57,9 @@ cat <<\EOF > its/rules/messages.its version="1.0"> - - + + EOF @@ -72,8 +73,7 @@ cat <<\EOF >messages.xml

This is a test message, with an element in a text

-

This is a test message, with an code in a text -$ echo foo >> /dev/null + $ echo ' ' >> /dev/null

@@ -104,8 +104,7 @@ msgstr "" #: messages.xml:9 #, no-wrap msgid "" -"\n" -"$ echo foo >> /dev/null\n" +" $ echo ' ' >> /dev/null\n" msgstr "" #. (itstool) path: messages/message@comment