From f8ee148f68823cf640db008b2984600d1e871d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 31 May 2018 10:19:55 +0200 Subject: [PATCH] Add XPath syntax highlighter to Rouge This is only used when building the site, like the Liquid extensions --- docs/_plugins/xpath_lexer.rb | 66 +++++++++++++++++++ .../pmd/docs/RuleDocGenerator.java | 2 +- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 docs/_plugins/xpath_lexer.rb diff --git a/docs/_plugins/xpath_lexer.rb b/docs/_plugins/xpath_lexer.rb new file mode 100644 index 0000000000..950ed6fc44 --- /dev/null +++ b/docs/_plugins/xpath_lexer.rb @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- # + +require 'rouge' + +module Rouge + module Lexers + class XPath < RegexLexer + + title "XPath" + desc "The XPath query language (http://en.wikipedia.org/wiki/XPath)" + tag 'xpath' + aliases 'xpath' + filenames '*.xpath' + mimetypes 'text/x-xpath' + + state :basic do + rule /\s+/, Text + rule /[(][:](?![)])/, Comment, :nested_comment + + rule /[\[\](){}|.,;!]/, Punctuation + + rule /"[^"]*+"/, Str::Double + rule /'[^']*+'/, Str::Single + + rule /\.\d++\b/, Num + rule /\b\d++\./, Num + rule /\b\d++(\.\d*+)?([eE][+-]?\d+)?\b/, Num + end + + state :operators do + rule /(<|>|=<|>=|==|\/\/|[\/*+-])(?=\s|[a-zA-Z0-9\[])/, Operator + # operators + rule /(or|and|not|mod|ne|eq|lt|le|gt|ge)/, Operator + # keywords + rule /some|in|satisfies|as|is|for|every|cast|castable|treat|instance|of|to|if|then|else|return|let|intersect|except|union|div|idiv/, Keyword::Reserved + # axes + rule /(self|child|attribute|descendant|descendant-or-self|ancestor|ancestor-or-self|following|following-sibling|namespace|parent|preceding-sibling)::/, Keyword::Namespace + # kind tests + rule /(node|document-node|text|comment|namespace-node|processing-instruction|attribute|schema-attribute|element|schema-element|function)\(\)/, Keyword::Reserved + # functions + rule /[a-zA-Z\-]+(?=\()/, Name::Function + end + + state :names do + rule /[a-zA-Z\-]+:/, Name::Namespace + rule /@[a-zA-Z][_\-a-zA-Z0-9]*/, Name::Attribute + rule /\$[a-zA-Z][_\-a-zA-Z0-9]*/, Name::Variable + # Node names + rule /[a-zA-Z]+/, Name::Tag + end + + state :root do + mixin :basic + mixin :names + mixin :operators + end + + state :nested_comment do + rule /[^(:)]+/, Comment + rule /\(:/, Comment, :push + rule /:\)/, Comment, :pop! + rule /[(:)]/, Comment + end + end + end +end \ No newline at end of file diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java index eb0e993901..2789a290f9 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java @@ -391,7 +391,7 @@ public class RuleDocGenerator { if (rule instanceof XPathRule || rule instanceof RuleReference && ((RuleReference) rule).getRule() instanceof XPathRule) { lines.add("**This rule is defined by the following XPath expression:**"); - lines.add("```"); + lines.add("```xpath"); lines.addAll(toLines(StringUtils.stripToEmpty(rule.getProperty(XPathRule.XPATH_DESCRIPTOR)))); lines.add("```"); } else {