This commit is contained in:
parent
7703d9c0ca
commit
2097939aee
36
tools/tagtool/README.md
Normal file
36
tools/tagtool/README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# Tagtool
|
||||
|
||||
## Example workflow
|
||||
|
||||
1. xsltproc tagsToCSV.xsl tags/.tags.en.xml | sort > tagsdata.csv
|
||||
2. edit tagsdata.csv
|
||||
3. tagtool.sh tagsdata.csv
|
||||
|
||||
## Preparing a csv file:
|
||||
|
||||
```
|
||||
xsltproc tagsToCSV.xsl tags/.tags.en.xml | sort > tagsdata.csv
|
||||
```
|
||||
|
||||
This processes the tags data and outputs the columns "action;name;id;section;count".
|
||||
|
||||
Columns:
|
||||
* action: empty, to be filled by you
|
||||
* name: value of the "content" attribute
|
||||
* id: the tag itself
|
||||
* section: news|events
|
||||
* count
|
||||
|
||||
"Section" and "count" are for additional context only.
|
||||
The "name" section comes at the beginning so that it is easier to find duplicate tags.
|
||||
|
||||
You can then fill the "action" field with the following actions:
|
||||
* mv:sometag
|
||||
rename the tag to sometag
|
||||
* rm
|
||||
remove the tag from all files
|
||||
|
||||
A word of warning: you'll probably need to polish the results by hand a little
|
||||
(removing empty lines, checking for duplicate tags introduced by renaming).
|
||||
|
||||
|
18
tools/tagtool/tagsToCSV.xsl
Normal file
18
tools/tagtool/tagsToCSV.xsl
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- create a csv file from the aggregated tags files in tags/
|
||||
|
||||
Usage to find duplicate tags:
|
||||
xsltproc tagsToCSV.xsl tags/.tags.en.xml |sort
|
||||
/-->
|
||||
<xsl:stylesheet version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:dt="http://xsltsl.org/date-time">
|
||||
<xsl:output method="text"
|
||||
encoding="utf-8"
|
||||
indent="yes"
|
||||
doctype-system="about:legacy-compat" />
|
||||
<xsl:template match="/">action;name;id;section;count
|
||||
<xsl:for-each select="tagset/tag">
|
||||
;<xsl:value-of select='.'/>;<xsl:value-of select='@name'/>;<xsl:value-of select='@section'/>;<xsl:value-of select='@count'/></xsl:for-each>;
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
107
tools/tagtool/tagtool.sh
Executable file
107
tools/tagtool/tagtool.sh
Executable file
@ -0,0 +1,107 @@
|
||||
#!/bin/bash
|
||||
# Tools to transform the tag data by bulk.
|
||||
# Copyright (C) 2019 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
TAGSDATAFILE="$1"
|
||||
if [[ ! -f "$1" ]]
|
||||
then
|
||||
echo "No data file. Please read the source for help..." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
findTaggedFiles()
|
||||
# findTaggedFiles TAG_ID
|
||||
# grep for files containing the tag
|
||||
{
|
||||
local tagId="$1"
|
||||
git grep -i -l ">$tagId</tag>"
|
||||
}
|
||||
|
||||
renameTag()
|
||||
# renameTag OLD NEW
|
||||
# rename the tag using case-insensitive matching in all files.
|
||||
{
|
||||
local oldTagId="$1"
|
||||
local newTagId="$2"
|
||||
echo "Renaming tag $oldTagId to $newTagId..." >&2
|
||||
for f in $(findTaggedFiles "$oldTagId")
|
||||
do
|
||||
echo "..$f" >&2
|
||||
if ! sed -E -i "s;>\W*$oldTagId\W*</tag>;>$newTagId</tag>;" "$f"
|
||||
then
|
||||
echo "ERROR!" >&2
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
removeTag()
|
||||
# removeTag TAG
|
||||
# remove the tag id in all files
|
||||
# this will result in additional empty lines
|
||||
{
|
||||
local tagId="$1"
|
||||
echo "Deleting tag $tagId..." >&2
|
||||
for f in $(findTaggedFiles "$oldTagId")
|
||||
do
|
||||
echo "..$f" >&2
|
||||
if ! sed -E -i "s;\W*<tag\W*content=\"[^\"]*\"\W*>\W*$TagId\W*</tag>\W*;;i" "$f"
|
||||
then
|
||||
echo "ERROR!" >&2
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
process_action()
|
||||
# process a single csv action line from stdin and call the appropriate method
|
||||
{
|
||||
IFS=";" read action name id section count || return 2
|
||||
# ignore empty actions
|
||||
if [[ -z "$action" ]]
|
||||
then
|
||||
return 0
|
||||
fi
|
||||
|
||||
case "$action" in
|
||||
rm)
|
||||
removeTag "$id"
|
||||
;;
|
||||
mv:*)
|
||||
renameTag "$id" "${action/mv:/}"
|
||||
;;
|
||||
*)
|
||||
echo "Ignoring action: $action on tag $section/$id" >&2
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
process_actions()
|
||||
{
|
||||
read firstline
|
||||
if [[ "$firstline" != "action;name;id;section;count" ]]
|
||||
then
|
||||
echo "Input data does not look like it contains the right columns. Bailing out..." >&2
|
||||
exit 1
|
||||
fi
|
||||
while process_action
|
||||
do
|
||||
true
|
||||
done
|
||||
}
|
||||
|
||||
process_actions < "$TAGSDATAFILE"
|
Loading…
Reference in New Issue
Block a user