Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XSLT extensions for beautification #139

Closed
spodzone opened this issue Aug 10, 2015 · 5 comments
Closed

XSLT extensions for beautification #139

spodzone opened this issue Aug 10, 2015 · 5 comments

Comments

@spodzone
Copy link

Hi,
This follows from the thread over at Glavin001/atom-beautify#480 (comment)

Desirable features for XSLT-specific beautification over and above XML:

  1. generally retain the XML-style 2-space indent (or 4-space, or whatever)

  2. blank lines between xsl-specific nodes, especially xsl:template, xsl:output

  3. blank lines before comments, especially comments immediately preceding an xsl:template

  4. indent attributes, especially when there's a long list of xmlns decls on an xsl:transform.

  5. double-check that it never breaks XML markup, especially regarding inserting bogus spaces in <xsl : text>.

Something like what this little shell-script achieves, except it doesn't get point 2 quite right:

#!/bin/sh

export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin

basic() {
  xmlstarlet fo -s 2
}

nltemplate() {
    sed -E 's#<xsl:template#\n  <xsl:template#g'
}

stylesheet() {
    sed -E '/<xsl:stylesheet/s# #\n    #g'
}

stylespace() {
    sed -E 's#<xsl:stylesheet#\n<xsl:stylesheet#'
}

paramspace() {
    sed -E '1,/<xsl:param/s#<xsl:(param)#\n  <xsl:\1#'
}

outputspace() {
    sed -E '1,/<xsl:output/s#<xsl:(output)#\n  <xsl:\1#'
}

variablespace() {
    sed -E '1,/<xsl:variable/s#<xsl:(variable)#\n  <xsl:\1#'
}

nowhitespaceblanklines() {
    sed -E 's#^[ \t]*$##'
}

basic | nltemplate | stylesheet | stylespace | paramspace | outputspace | variablespace | nowhitespaceblanklines
@prettydiff
Copy link
Owner

Thanks for opening the issue. Would you also be kind of to create a gist with a complete XSLT code sample that includes a bunch of possible edge cases? All that you asked for is easily achievable, but I want to make sure I get it right and do not introduce any harmful regression along the way.

@spodzone
Copy link
Author

No worries. See https://gist.github.com/spodzone/675e40505b70c34fe5ee
I think that's survived intact (md5sum==5b68157d09dba0fa1ed1e53bb5bf0bb1). It's not really in use anywhere, but does contain several aspects I like in more realistic code.

No doubt some folks consider me a heathen for some of these, but a few things I would particularly draw your attention to:

  • The indents on lines 3-9, 12-16, 34-56
  • The blank line before comments attached to a subsequent xsl:template (lines 17, 25, 30 etc)
  • the space at the end of self-closing tags />
  • the uses of xsl:text between lines 68-73

Thanks!

@prettydiff
Copy link
Owner

Here is what I can do:

  • a new option to always force an empty line above a comment (or run of comments)
  • a new option to always indent attributes, each on its own line
  • a new option to impose a space before />
  • treat xsl:text exactly like HTML's pre
  • force a blank line above tags with the xsl namespace
  • I won't mess with indentation definitions (2 spaces) as this can already be customized
  • if the root element has the xsl namespace then arbitrarily set these new options to true

@spodzone
Copy link
Author

Yep, that's a pretty good start. The n-space indent should be taken from the global config, agreed.

We might need to be careful about two of the options: I don't always newline+indent attributes, as the ul/li/a subtree shows. It's only when the line is going to wrap otherwise, then I will. Second: "force a blank line above xsl tags", well, careful how that interacts with forcing an empty line around comments...

I was thinking this morning:
0) blank lines are truly blank - not a space or tab on them - so I can bounce on { and } in vi if I have to

  1. XSLT (at least in my world) tends to be a section of preparatory / header blocks and then a load of templates, where the headers are groups of xsl:include URIs and some xsl:param and some xsl:variable definitions, each of which forms a tight paragraph.

It would make an interesting test to join all the above XSLT into one long line, beautify it and see what falls out.

@prettydiff prettydiff modified the milestones: v1.12.21, v1.12.20, v1.13.1, v1.13.2 Aug 15, 2015
@prettydiff
Copy link
Owner

This is available for you to test out via my web tool. http://prettydiff.com/ignore/testlocation/prettydiff.com.xhtml?m=beautify

I created two new options:

  • commline - Forces a new line above comments
  • spaceclose - Forces self-closing tags to close with a space: " />"

You can find these options if you scroll down the page and look for the markup specific options. I am not actually setting these options to true yet, but I am automatically putting newlines above xsl tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants