Skip to content

Commit fffef94

Browse files
authored
Format XML with xsltproc (#30)
1 parent 9d99eb6 commit fffef94

7 files changed

Lines changed: 62 additions & 3 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*
22
!.editorconfig
33
!entry.ts
4+
!stylesheet.xml

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN apk add --no-cache --virtual .build-deps \
88
python3-dev \
99
&& apk add --no-cache \
1010
clang-extra-tools \
11+
libxslt \
1112
nodejs \
1213
openjdk11-jre-headless \
1314
py3-pip \
@@ -19,6 +20,7 @@ RUN apk add --no-cache --virtual .build-deps \
1920
click==8.0.4 \
2021
isort==5.9.3 \
2122
&& npm install -g \
23+
@prettier/plugin-xml@2.0.1 \
2224
@types/node@16.6.2 \
2325
prettier@2.3.2 \
2426
svgo@1.3.2 \

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
This repo currently contains a single [pre-commit](https://pre-commit.com/) hook that internally runs several code formatters in parallel.
44

5-
- [Prettier](https://github.com/prettier/prettier) v2.3.2 for CSS, HTML, JS, JSX, Markdown, Sass, TypeScript, YAML
5+
- [Prettier](https://github.com/prettier/prettier) v2.3.2 for CSS, HTML, JS, JSX, Markdown, Sass, TypeScript, XML, YAML
66
- [Black](https://github.com/psf/black) v21.7b0<!-- TODO: The next time we upgrade Black, we should also address the isort comment in .editorconfig --> for Python
77
- [autoflake](https://github.com/myint/autoflake) v1.4 for Python
88
- [isort](https://github.com/PyCQA/isort) v5.9.3 for Python
99
- [google-java-format](https://github.com/google/google-java-format) v1.11.0 for Java
1010
- [ktfmt](https://github.com/facebookincubator/ktfmt) v0.28 for Kotlin
1111
- [scalafmt](https://scalameta.org/scalafmt/) v2.7.5 for Scala
1212
- [shfmt](https://github.com/mvdan/sh) v3.3.1 for Shell
13+
- [xsltproc](http://www.xmlsoft.org/xslt/xsltproc.html) from libxslt v10135 for XML
1314
- [terraform fmt](https://github.com/hashicorp/terraform) v0.11.14 and v0.12.29 for Terraform
1415
- [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) v11.1.0 for Protobuf
1516
- [SVGO](https://github.com/svg/svgo) v1.3.2 for SVG

entry.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const enum HookName {
7979
Shfmt = "shfmt",
8080
Svgo = "SVGO",
8181
TerraformFmt = "terraform fmt",
82+
Xsltproc = "xsltproc",
8283
}
8384

8485
/** Arguments passed into this hook via the `args` pre-commit config key */
@@ -217,8 +218,8 @@ const HOOKS: Record<HookName, LockableHook> = {
217218
"all",
218219
...sources,
219220
),
220-
include: /\.(css|html?|markdown|md|scss|tsx?|ya?ml)$/,
221-
runAfter: [HookName.Sed],
221+
include: /\.(css|html?|markdown|md|scss|tsx?|xml|ya?ml)$/,
222+
runAfter: [HookName.Sed, HookName.Xsltproc],
222223
}),
223224
[HookName.Scalafmt]: createLockableHook({
224225
action: sources =>
@@ -396,6 +397,16 @@ const HOOKS: Record<HookName, LockableHook> = {
396397
include: /\.tf$/,
397398
runAfter: [HookName.Sed],
398399
}),
400+
[HookName.Xsltproc]: createLockableHook({
401+
action: sources =>
402+
Promise.all(
403+
sources.map(source =>
404+
run("xsltproc", "--output", source, "/stylesheet.xml", source),
405+
),
406+
),
407+
include: /\.xml$/,
408+
runAfter: [HookName.Sed],
409+
}),
399410
};
400411

401412
/** Files that match this pattern should never be processed */

stylesheet.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<xsl:stylesheet
3+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4+
xmlns:android="http://schemas.android.com/apk/res/android"
5+
version="1.0"
6+
>
7+
<!-- Indent output -->
8+
<xsl:strip-space elements="*" />
9+
<xsl:output encoding="utf-8" indent="yes" method="xml" />
10+
<!-- Document root -->
11+
<xsl:template match="/">
12+
<xsl:apply-templates />
13+
</xsl:template>
14+
<!-- Sort non-text nodes' attributes -->
15+
<xsl:template match="*">
16+
<xsl:copy>
17+
<xsl:for-each select="@*">
18+
<xsl:sort select="name(.)" />
19+
<xsl:copy />
20+
</xsl:for-each>
21+
<xsl:apply-templates />
22+
</xsl:copy>
23+
</xsl:template>
24+
<!-- Leave text nodes untouched -->
25+
<xsl:template match="text()|comment()|processing-instruction()">
26+
<xsl:copy />
27+
</xsl:template>
28+
</xsl:stylesheet>

test/after/hello.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<androidx.constraintlayout.widget.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_height="wrap_content"
7+
android:layout_width="match_parent"
8+
>
9+
<TextView
10+
android:id="@+id/body"
11+
android:layout_width="wrap_content"
12+
style="@style/foobar"
13+
tools:text="Hello world"
14+
/>
15+
</androidx.constraintlayout.widget.ConstraintLayout>

test/before/hello.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="utf-8" ?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"><TextView style="@style/foobar" android:id="@+id/body" android:layout_width="wrap_content" tools:text="Hello world" /></androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)