AsciiDoctor: How can I add custom xmlns' - asciidoctor

How can I add a custom xmlns in the output when I convert an asciidoc file with AsciiDoctor?
I'd like to add xmlns:xi="http://www.w3.org/2001/XInclude" in the top book tag.
The current implementation seems to generate:
<?xml version="1.0" encoding="UTF-8"?>
<?asciidoc-toc?>
<?asciidoc-numbered?>
<book xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink" version="5.0" xml:lang="en">
<info>
<title>title</title>
</info>
</book>
from this:
= title
:lang: en
When I run:
$ asciidoctor -b docbook5 -d book -o out.xml source.txt
There is a built-in attribute xmlns, but it seems to be for docbook 4.5.
The reason I want to use XInclude is to include some xml files from Docinfo files and Passthrough Blocks

With a bit of research inside the asciidoctor code it quickly became clear that the part you'd like to modify is fairly static.
See asciidoctor/converter/docbook5.rb Line 44 for more info.
The best approach is to create a postprocessor extension which modifies the output. The example below is just to show a possible implementation.
Create a file with the following content and call it docbook_postprocessor.rb.
class Docbook5XiPostprocessor < Asciidoctor::Extensions::Postprocessor
def process document, output
if document.basebackend? 'docbook'
input_regex = %r{^(<.*xmlns:xl="http://www.w3.org/1999/xlink") (version="5.0".*>)}
replacement = %(\\1 xmlns:xi="http://www.w3.org/2001/XInclude" \\2)
output = output.sub(input_regex, replacement)
end
output
end
end
Asciidoctor::Extensions.register do
postprocessor Docbook5XiPostprocessor
end
Note: The above extension is for the sake of brevity placed in the same directory as the asciidoctor source file called source.adoc.
The run the asciidoctor command with the -r ./docbook_postprocessor.rb parameters.
$ asciidoctor -r ./docbook_postprocessor.rb -b docbook5 -d book -o - source.adoc
<?xml version="1.0" encoding="UTF-8"?>
<?asciidoc-toc?>
<?asciidoc-numbered?>
<book
xmlns="http://docbook.org/ns/docbook"
xmlns:xl="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
version="5.0"
xml:lang="en">
<info>
<title>test</title>
<date>2020-12-19</date>
</info>
</book>
* Above output has been slightly reformatted to eliminate the scrollbar
Creating ruby gem with the above code for easier distribution is a task left to the reader.

Related

Reading source files in MSVS project using xmlstarlet

I am trying to get the source files and include directories from a vcxproj file. Eg of vcsproj:
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
....
<ItemGroup>
<ClCompile Include="$(SrcDir)d1\f1cpp" />
<ClCompile Include="$(SrcDir)d2\f2.cpp" />
</ItemGroup>
...
</Project>
I tried this:
xmlstarlet sel -t -v "//_:ItemGroup/ClCompile/#Include" myProj.vcxproj but didn't work.
However, when I tried this (copying the code from some page that I came across), it works:
echo '<?xml version="1.0" encoding="utf-8"?> <ELEMENT xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<SUB_ELEMENT attribute="attr_value"/>
</ELEMENT>' | xmlstarlet sel -t -v '//_:SUB_ELEMENT/#attribute' --nl
o/p: attr_value
I don't see how the two are different with respect to reading an attribute value from an xml with a namespace. I further tried a stripped down version of the vcxproj file:
echo '<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="$(SrcDir)d1\f1cpp" /> </ItemGroup> </Project>' | xmlstarlet sel -t -v '//_:ItemGroup/#Include' --nl
o/p: <no o/p>
Any indication on why it is not working or how to get this to work would be very helpful.
Edit: Expected output from the vcxproj would be a list of filenames. For the above command it would be $(SrcDir)d1\f1cpp
Any indication on why it is not working or how to get this to work would be very helpful
Since you're using the default namespace add the _:
shortcut on the node test in each
location step,
and the -T (--text)
option to make text mode output:
xmlstarlet select -T -t -v "//_:ItemGroup/_:ClCompile/#Include" -n file.xml

Is it possible to use unicode character within glob pattern in mime definition file?

I'm issuing the command xdg-mime install nv-custom.xml - using this file:
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="text/x-customtest">
<comment></comment>
<glob weight="60" pattern="*...someUnicodeHere"/>
</mime-type>
</mime-info>
Is it possible to use unicode in the glob's pattern field?
I tried Ctrl + Shift + U followed by a code, which works fine, but then the file does not have any effect, whereas it works just fine with "regular" text.

How to sort strings.xml alphabetically in Android Studio

Hello is there a way to sort the string names alphabetically in strings.xml ?
It should sort it like this
Before
<string name="ccc">CText</string>
<string name="aaa">AText</string>
<string name="bbb">BText</string>
After
<string name="aaa">AText</string>
<string name="bbb">BText</string>
<string name="ccc">CText</string>
I am using Android Studio 1.5.1
I used AndroidXmlSorter plugin and it works perfectly for me.
How to install:
Go to Android Studio -> Preferences -> Plugins and hit Browse repositories..
Search for AndroidXmlSorter, install and restart your Android Studio. Go to your strings.xml file and hit Ctrl+L. Voila.
Disclaimer: I'm not the author of this plugin, I just found it by chance and I think all the credits should go to the plugin author. The repo is here: https://github.com/roana0229/android-xml-sorter
For anyone else who bumps into this. Copy all the string elements, paste into an Excel spreadsheet sort A-Z and then copy and paste back.
[Sort Android strings.xml in Alphabetical Order]
There is a plugin called Lines Sorter which can sort selected lines or whole files.
2022-09 | Simple plugin free solution:
Android studio includes option to sort lines / reverse lines.
Highlight lines of code to be sorted
unsorted lines
Edit > sort lines or Edit > reverse lines
sort line
Enjoy (reverse) alphabetical order
sorted lines
Sadly AndroidXmlSorter doesn't work for me when sorting multi-line strings.
My solution: Sort the strings.xml according to my android_strings_format.xslt (with xsltproc) and reformat it (with xmllint).
Cons: You have to execute a script. Therefore, it is not a true native feature of Android Studio. But you can easily execute scripts in Android Studio.
The script:
#!/bin/sh
sort_xml_file() {
xsltproc --output "$1" android_strings_format.xslt "$1"
export XMLLINT_INDENT=" "
xmllint --format --encode "utf-8" --output "$1" "$1"
}
sort_xml_file "src/main/res/values/strings.xml"
The android_strings_format.xslt file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
version="1.0"
encoding="UTF-8"
indent="yes"
omit-xml-declaration="yes"/>
<xsl:template match="/resources">
<resources>
<xsl:for-each select="string">
<xsl:sort select="#name"/>
<string name="{#name}">
<xsl:copy-of select="#* | node()"/>
</string>
</xsl:for-each>
</resources>
</xsl:template>
</xsl:stylesheet>
Works on Manjaro Linux x64.
EDIT: I have found a way to prevent multi-line text. I can enclose the text with quotes. <string name="available_version_error">"<u>ERRORa</u>"</string>
I used a XML formatter with an xslt definition - but this was too complex if you also uses plurals.
My solution: writing my own Python script:
import re
# add your files here
strings_files = ["path/to/strings.xml"]
# 1. transform the strings.xml file to a dict
# 2. sort the dict
# 3. transform the dict to a strings.xml file
def sort_strings_xml_file(path_to_file: str):
entries = dict()
current_entry_name = None
# read entries from strings.xml file
with open(path_to_file, "r") as file:
for line in file.readlines():
# a new entry was found in the strings.xml file
if line.strip().startswith("<string ") or line.strip().startswith("<plurals "):
current_entry_name = re.search(r'name="(.+)"', line).group(1)
entries[current_entry_name] = ""
# store content for the current entry
if current_entry_name is not None:
entries[current_entry_name] += line
# stop recording for the current entry
if line.strip().endswith("</string>") or line.strip().endswith("</plurals>"):
current_entry_name = None
entries = dict(sorted(entries.items()))
# write results back to the strings.xml file
with open(path_to_file, "w") as file:
file.write(('<?xml version="1.0" encoding="utf-8"?>'
'<resources>'
f'{"".join(entries.values())}</resources>'))
print(f"{path_to_file} was sorted")
for strings_file in strings_files:
sort_strings_xml_file(strings_file)
i would turn them to ASCII then sort that one and turn them back to string, very easy and very effective
Update: Easiest way is: Ctrl+A then Ctrl+Alt+L
For configuration: In Android studio, you can quickly sort XML code by following step:
Select all XML code in a file by Ctrl+A
Use combination: Ctrl+Alt+Shift+L
Select "Selected text" + "Rearrange code" Then Press RUN

Define a Linux manual page's TITLE text when using docbook2man?

I'm experimenting with Linux manual (man) page creation using DocBook, and specifically I'm using 'docbook2man' on a Fedora 20 box, and I've been unable to figure how to create the manual's title text.
For example, if I open the man-pages(7) manual page, the manual's title is MAN-PAGES(7) and the manual's title text is Linux Programmer's Manual.
For further clarification, man-pages(7) defines the TH command as
.TH title section date source manual
It's the manual element--e.g., Linux Programmer's Manual--that I'm trying to figure out how to create using docbook2man.
I've been experimenting with the example code found in section 4.6 "Generating a man page" on the Using DocBook website. The pertinent sections of that code example are provided below (see Listing 1). The file name I'm using for this example code is foo-ref.sgml. The command line I'm using is
docbook2man foo-ref.sgml
Listing 1. Example SGML manual page
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
<refentry>
<refentryinfo>
<date>2001-01-01</date>
</refentryinfo>
<refmeta>
<refentrytitle>
<application>foo</application>
</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo>foo 1.0</refmiscinfo>
</refmeta>
<refnamediv>
<refname>
<application>foo</application>
</refname>
<refpurpose>
Does nothing useful.
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<refsynopsisdivinfo>
<date>2001-01-01</date>
</refsynopsisdivinfo>
<cmdsynopsis>
<command>foo</command>
<arg><option>-f </option><replaceable class="parameter">bar</replaceable></arg>
<arg><option>-d<replaceable class="parameter">n</replaceable></option></arg>
<arg rep="repeat"><replaceable class="parameter">file</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<refsect1info>
<date>2001-01-01</date>
</refsect1info>
<title>DESCRIPTION</title>
<para>
<command>foo</command> does nothing useful.
</para>
</refsect1>
<!-- etc. -->
</refentry>
When I process this source code with docbook2man, a man page named 'foo.1' is generated whose .TH macro is rendered as shown below, but with an empty string "" for the manual's title text element:
.TH "FOO" "1" "2001-01-01" "foo 1.0" ""
I've dug around in the DocBook5 refentry refernece manual, trying various tags, but so far I haven't found anything that produces the title text. I've also searched the Interwebs for DocBook manual page examples, but none of the examples I've found produces the manual title text. So I'm starting to wonder if this is even doable with docbook2man?
Any suggestions?
After some more experimenting and without the desired results, I decided to abandon the docbook2man program and use instead the doclifter(1) and xsltproc(1) programs to create my Linux manual pages.
Example 1) On a Fedora 20 host, use doclifter to translate an existing man page man-pages(7) into a DocBook 'refentry' compatible XML file. This is VERY USEFUL because it renders an XML file that can can be used as a example reference for creating one's own DocBook 'refentry' XML source files.
Listing 1. doclifter example (man->XML)
# Install the doclifter package
sudo yum -y install doclifter
# Create a temporary directory in which to experiment, and go to it
mkdir ~/tmp
cd ~/tmp/
# Copy the existing man-pages.7.gz file into the temporary directory
# and uncompress it.
cp /usr/share/man/man7/man-pages.7.gz ~/tmp/
gunzip man-pages.7.gz
# Convert the man page into DocBook XML format
doclifter man-pages.7
# There should now be a file named 'man-pages.7.xml'.
ls man-pages.7.xml
Example 2) On a Fedora Linux 20 host, use xsltproc to convert an XML file ~/tmp/foo.xml containing DocBook refentry content into a Linux manual (man) page ~/tmp/foo.1.
Listing 2 below is the XML source code for example file ~/tmp/foo.xml that will be translated by xsltproc into the manual page file ~/tmp/foo.1 (see Listing 3 below). This XML source code is a derivative work from (1) the SGML example code provided in section 4.6 "Generating a man page" on the Using DocBook website, and (2) various code snippets--some modified, some copied verbatim--from the XML file created by doclifter in Example 1 above.
Listing 2. Example DocBook 'refentry' XML file ~/tmp/foo.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<refentry id='foo'>
<!-- HEADER & FOOTER INFO -->
<refmeta>
<!-- see also: man 7 man-pages -->
<!-- .TH title section date source manual -->
<!-- title -->
<refentrytitle>FOO</refentrytitle>
<!-- section -->
<manvolnum>1</manvolnum>
<!-- date -->
<refmiscinfo class='date'>2015-06-26</refmiscinfo>
<!-- source -->
<refmiscinfo class='source'>SOURCE TEXT</refmiscinfo>
<!-- manual -->
<refmiscinfo class='manual'>MANUAL TEXT</refmiscinfo>
</refmeta>
<!-- Section: NAME -->
<refnamediv>
<refname>foo</refname>
<refpurpose>
Does nothing useful.
</refpurpose>
</refnamediv>
<!-- Section: SYNOPSYS -->
<refsynopsisdiv id='synopsis'>
<cmdsynopsis>
<command>foo</command>
<arg><option>-f </option><replaceable class="parameter">bar</replaceable></arg>
<arg><option>-d<replaceable class="parameter">n</replaceable></option></arg>
<arg rep="repeat"><replaceable class="parameter">file</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>
<!-- Section: DESCRIPTION | OPTIONS | ... -->
<refsect1 id='description'><title>DESCRIPTION</title>
<para>
<command>foo</command> does nothing useful.
</para>
</refsect1>
<refsect1 id='authors'><title>AUTHORS</title>
<para>Jim Fischer</para>
</refsect1>
</refentry>
Listing 3 below shows the xsltproc commands to use (on a Fedora 20 host) to translate the file ~/tmp/foo.xml into the manual page file ~/tmp/foo.1.
Listing 3. xsltproc example (foo.xml -> foo.1)
# Change to the temporary directory
cd ~/tmp/
# Use xsltproc to convert foo.xml into foo.1
$ xsltproc /usr/share/sgml/docbook/xsl-stylesheets-1.78.1/manpages/docbook.xsl foo.xml
# Verify foo.1 was created
ls foo.1
man ./foo.1
Still experimenting with this whole XML->MAN conversion process. Here's another method for performing the conversion that I think I prefer to the xsltproc version shown in my previous reply.
Listing 1. XML->MAN conversion using db2x_xsltproc and db2x_manxml
#!/bin/bash
# vim: ft=sh:tw=75:ts=4:sw=4
clear
MANDIR=${HOME}/tmp/
MANUAL=foo
SECTION=1
MAN_FILE="${MANUAL}.${SECTION}"
XML_FILE="${MANUAL}.xml"
cd "${MANDIR}"
# [Optional] Sanity check to ensure the man page build actually occurs.
rm -f "${MAN_FILE}"
# XML -> MAN
# n.b. The option '--to-stdout' allows full control over the
# man page's file name. Otherwise, the man page's file name is
# defined implicitly via the refentry.refmeta.refentrytitle
# value in the XML source file. Note that (1) the refentrytitle
# value corresponds to ".TH title" and should be written in
# all-caps--e.g., 'FOO'--in accordance with man-pages(7), and
# (2) one typically does not want the man page's file name to
# be all-caps (preferred file name is 'foo.1' and not 'FOO.1').
#
db2x_xsltproc -s man "${XML_FILE}" | db2x_manxml --to-stdout > "${MAN_FILE}"
exit_code=("${PIPESTATUS[#]}")
if [ ${exit_code[0]} -ne 0 ]; then
echo ":: ERROR :: db2x_xlstproc returned exit code ${exit_code[0]}; aborting..." >&2
exit "${exit_code[0]}"
elif [ ${exit_code[1]} -ne 0 ]; then
echo ":: ERROR :: db2x_manxml returned exit code ${exit_code[1]}; aborting..." >&2
exit "${exit_code[1]}"
fi
# Display the man file
man ./"${MAN_FILE}"

Automatizing 'simplify path' for a svg-file (using inkscape)

I would like to automatize the inkscape command "simplify path". Concretely, I would like a command line tool which takes a svg-file as input, applies "simplify path" to all paths in the figure and saves a new (smaller) svg-file. Is this possible using inkscape? Is there a free command line tool (I'm using linux) which does the job?
UPDATE:
Since the question/answer is quite old the inkscape command line changed.
inkscape file.svg --batch-process --actions='EditSelectAll;SelectionSimplify;FileSave;FileClose'
Also see comment of Oren Ben-Kiki or Pix answer.
ORIG:
Should be possible:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine.html
shows how to call functions of inkscape (called "verbs") from the command line. To get a list of all verbs call inkscape --verb-list on commandline. What you are looking for is SelectionSimplify.
Therefore you have to write a small script that is filtering every id out of the svg and is calling inkscape with the ids. Something like this (optimize all pathes and quit from inkscape at the end)
inkscape filename.svg --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose --verb=FileQuit
Extending from Fabian's answer, to control the threshold of the simplification function, I found I needed to make a fake home directory with a minimal preferences file containing my desired threshold. Here is a simple script I just put together.
simplify.sh:
#!/bin/bash
FILENAME=$1
THRESHOLD=$2
FAKEHOME=$(mktemp -d)
mkdir -p $FAKEHOME/.config/inkscape
cat > $FAKEHOME/.config/inkscape/preferences.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<inkscape
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1">
<group
id="options">
<group
id="simplifythreshold"
value="${THRESHOLD}" />
</group>
</inkscape>
EOF
# for Inkscape < 1.0
#HOME=$FAKEHOME inkscape $FILENAME --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose
# for Inkscape > 1.0
HOME=$FAKEHOME inkscape --with-gui --batch-process $FILENAME --verb='EditSelectAll;SelectionSimplify;FileSave'
#rm -rf $FAKEHOME
Alternative to Inkscape
I've got much better results using SVGO (reduced a file from 2.7 MB to 350 KB).
You may use this online service for individual files: https://jakearchibald.github.io/svgomg/

Resources