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

GeekoDoc Vim File #16

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions geekodoc/rng/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#
# Requirements:
# * trang
# * rng2srng
# * docbook_5
# * python3-rnginline (from obs://home:thomas-schraitle/python3-rnginline)
# * python3-rnginline (from obs://devel:languages:python3/python3-rnginline)


.SUFFIXES: .rng rnc
Expand All @@ -20,13 +21,12 @@ DOCBOOKXI_RNG := $(patsubst .rnc,.rng,$(DOCBOOKXI_RNC))

.PHONY: all clean

all: $(SUSESCHEMA)-flat.rnc
all: $(SUSESCHEMA)-flat.rnc $(SUSESCHEMA)-flat.rng $(SUSESCHEMA).vim

clean:
rm $(SUSESCHEMA)-flat.rnc $(DOCBOOKXI_RNC) $(DOCBOOKXI_RNG) 2>/dev/null || true


.INTERMEDIATE: $(SUSESCHEMA).rng $(SUSESCHEMA)-flat.rni $(SUSESCHEMA)-flat.rng
rm $(SUSESCHEMA)-flat.rn{c,g} \
$(SUSESCHEMA).vim \
$(DOCBOOKXI_RNC) $(DOCBOOKXI_RNG) 2>/dev/null || true


$(DOCBOOKXI_RNC): $(DOCBOOKXI_RNC_PATH)
Expand All @@ -37,6 +37,7 @@ $(SUSESCHEMA).rng: $(SUSESCHEMA).rnc $(DOCBOOKXI_RNC)
@echo "* Converting $< -> $@"
trang $< $@

.INTERMEDIATE: $(SUSESCHEMA)-flat.rni
$(SUSESCHEMA)-flat.rni: $(SUSESCHEMA).rng
@echo "* Flattening $< -> $@"
rnginline $< $@
Expand All @@ -50,3 +51,11 @@ $(SUSESCHEMA)-flat.rnc: $(SUSESCHEMA)-flat.rng
trang $< $@
@sed -i -r 's_\s+$$__' $@

# .INTERMEDIATE: $(SUSESCHEMA)-flat.srng
$(SUSESCHEMA)-flat.srng: $(SUSESCHEMA)-flat.rng
echo '* Creating Simplified RNG version'
rng2srng $< | xmllint --format - > $@

$(SUSESCHEMA).vim: $(SUSESCHEMA)-flat.srng
echo '* Creating VIM XML Omni Datafile...'
xsltproc -o $@ ../../rng2vim.xsl $<
254 changes: 254 additions & 0 deletions rng2vim.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

-->
<xsl:stylesheet version="1.0"
xmlns:rng = "http://relaxng.org/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
xmlns:s="http://purl.oclc.org/dsdl/schematron"
xmlns:d = "http://docbook.org/ns/docbook"
xmlns:xlink = "http://www.w3.org/1999/xlink"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:key name="rng.element"
match="rng:define[rng:element[@name] or rng:element[rng:name]]"
use="@name"/>

<xsl:key name="rng.attribute"
match="rng:define[rng:attribute[@name] or rng:attribute[rng:name]]"
use="@name"/>

<xsl:key name="rng.define" match="rng:define" use="@name"/>

<xsl:param name="schemaname">geekodoc5</xsl:param>

<xsl:variable name="xml.ns">http://www.w3.org/XML/1998/namespace</xsl:variable>
<xsl:variable name="xlink.ns">http://www.w3.org/1999/xlink</xsl:variable>


<!-- =================================================================== -->
<xsl:template name="vim.header">
<xsl:text>" Vim XML data file
" Language: GeekoDoc5
" Maintainer: Thomas Schraitle &lt;tom@opensuse.org>
" URL: https://github.com/openSUSE/geekodo
" Last Change: November 2016
" Description: An XML data file for the GeekoDoc5 markup language.

let g:xmldata_</xsl:text>
<xsl:value-of select="$schemaname"/>
<xsl:text> = {&#10;</xsl:text>
<xsl:text>\ 'vimxmlentities': [''],&#10;</xsl:text>
</xsl:template>

<xsl:template name="vim.footer">
<xsl:text>}&#10;</xsl:text>
<xsl:text>" vim:ft=vim:ff=unix&#10;</xsl:text>
</xsl:template>


<!-- =================================================================== -->
<xsl:template match="/">
<xsl:call-template name="vim.header"/>

<xsl:apply-templates select=".//rng:start[1]"/>
<!--<xsl:apply-templates select=".//rng:define[rng:element/@name]">
<xsl:sort select="rng:element/@name"/>
</xsl:apply-templates>-->

<xsl:call-template name="vim.footer"/>
</xsl:template>

<xsl:template match="rng:start">
<xsl:variable name="refs.inside.start" select=".//rng:ref"/>
<xsl:call-template name="start.elements">
<xsl:with-param name="refnodes" select="$refs.inside.start"/>
</xsl:call-template>
<!-- Find all element definitions -->
<xsl:apply-templates select="//rng:define[rng:element[rng:name]]"><!-- rng:ref -->
<xsl:sort select="rng:element/rng:name"/><!-- current()/@name -->
</xsl:apply-templates>
</xsl:template>

<xsl:template name="start.elements">
<xsl:param name="refnodes"/>
<xsl:message>Found <xsl:value-of select="count($refnodes)"/> allowed elements for start.</xsl:message>
<xsl:variable name="tmp.rootnodes">
<xsl:for-each select="$refnodes">
<xsl:sort select="./@name"/>
<xsl:variable name="rng.element.node" select="key('rng.element', current()/@name)"/>
<xsl:variable name="ref.name" select="($rng.element.node/rng:element/@name |
$rng.element.node/rng:element/rng:name)[1]"/>
<xsl:if test="$ref.name != ''">
<xsl:value-of select='concat("&apos;", $ref.name, "&apos;")'/>
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="rootnodes">
<xsl:variable name="root" select="normalize-space($tmp.rootnodes)"/>
<xsl:variable name="lastchar" select="substring($root,string-length($root),1)"/>
<xsl:choose>
<xsl:when test="$lastchar = ','">
<xsl:value-of select="substring($root, 1, string-length($root)-1)"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$root"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:text>\ 'vimxmlroot': [</xsl:text>
<xsl:value-of select="$rootnodes"/>
<xsl:text>],&#10;</xsl:text>
</xsl:template>

<xsl:template match="rng:ref">
<xsl:variable name="rng.element.node" select="key('rng.element', @name)"/>
<xsl:variable name="rng.element.name" select="($rng.element.node/rng:element/@name
|$rng.element.node/rng:element/rng:name)[1]"/>
<!-- <xsl:variable name="rng.attribute.node" select="key('rng.attribute', @name)"/>
<xsl:variable name="rng.attribute.name" select="($rng.element.node/rng:attribute/@name
|$rng.element.node/rng:attribute/rng:name)[1]"/>
-->
<xsl:message> ref: <xsl:value-of select="@name"/>:<xsl:value-of select="$rng.element.name"/></xsl:message>

<xsl:text>'</xsl:text>
<xsl:value-of select="$rng.element.name"/>
<xsl:text>'</xsl:text>

<!-- <xsl:choose>
<xsl:when test="$rng.element.node">
<xsl:value-of select='concat("\ &apos;", $rng.element.name, "&apos;: [&#10;")'/>
<!-\- TODO: Add here child elements -\->

<xsl:text>\ {</xsl:text>
<xsl:for-each select="$rng.element.node//rng:attribute">
<xsl:variable name="attr.ns" select="(current()/rng:name/@ns | current()/@ns)[1]"/>
<xsl:variable name="attr.name" select="(current()/rng:name | current()/@name)[1]"/>
<xsl:variable name="attr.name.prefix">
<xsl:choose>
<xsl:when test="$attr.ns = $xml.ns">xml:</xsl:when>
<xsl:when test="$attr.ns = $xlink.ns">xlink:</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:variable>
<xsl:message> attr:<xsl:value-of select="concat($attr.name.prefix, $attr.name)"/></xsl:message>
<xsl:value-of select='concat("&apos;", $attr.name.prefix, $attr.name, "&apos;: ")'/>
<xsl:text>[</xsl:text>
<xsl:if test="current()//rng:value">
<xsl:for-each select="current()//rng:value">
<xsl:value-of select='concat("&apos;", ., "&apos;")'/>
<xsl:if test="position() &lt; last()">, </xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:text>], </xsl:text>
</xsl:for-each>
<xsl:text>}&#10;</xsl:text>
<xsl:text>\ ],&#10;</xsl:text>
</xsl:when>
<xsl:when test="$rng.attribute.node">
<xsl:value-of select="$rng.attribute.name"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>WARN: Can't find appropriate node for <xsl:value-of select="@name"/></xsl:message>
<xsl:variable name="def" select="key('rng.define', @name)"/>
</xsl:otherwise>
</xsl:choose>-->
</xsl:template>


<xsl:template match="rng:element">
<xsl:value-of select='concat("\ ", @name, ": [&#10;")'/>
</xsl:template>

<xsl:template match="rng:define[rng:element[rng:name]]">
<xsl:variable name="rng.element.name" select="(rng:element/@name
|rng:element/rng:name)[1]"/>
<xsl:message>Found <xsl:value-of select="$rng.element.name"/></xsl:message>

<xsl:text>\ </xsl:text>
<xsl:value-of select='concat("&apos;", $rng.element.name, "&apos;")'/>
<xsl:text>: [&#10;</xsl:text>

<!-- Possible child elements -->
<!-- \ ['title', 'para'],-->
<xsl:text>\ [</xsl:text>
<xsl:for-each select=".//rng:ref">
<xsl:apply-templates select="."/>
<xsl:if test="position() &lt; last()">, </xsl:if>
</xsl:for-each>
<xsl:text>],&#10;</xsl:text>

<!-- Possible attributes -->
<xsl:text>\ {</xsl:text>
<xsl:for-each select=".//rng:attribute">
<xsl:variable name="attr.ns" select="(rng:name/@ns | @ns)[1]"/>
<xsl:variable name="attr.name" select="(rng:name | @name)[1]"/>
<xsl:variable name="attr.name.prefix">
<xsl:choose>
<xsl:when test="$attr.ns = $xml.ns">xml:</xsl:when>
<xsl:when test="$attr.ns = $xlink.ns">xlink:</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:variable>
<xsl:message> attr:<xsl:value-of
select="concat($attr.name.prefix, $attr.name)"/></xsl:message>
<xsl:value-of
select='concat("&apos;", $attr.name.prefix, $attr.name, "&apos;: ")'/>
<xsl:text>[</xsl:text>
<xsl:if test="current()//rng:value">
<xsl:for-each select="current()//rng:value">
<xsl:value-of select='concat("&apos;", ., "&apos;")'/>
<xsl:if test="position() &lt; last()">, </xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:text>], </xsl:text>
</xsl:for-each>
<xsl:text>}&#10;</xsl:text>
<xsl:text>\ ],&#10;</xsl:text>
</xsl:template>

<xsl:template match="rng:attribute">

</xsl:template>

<xsl:template match="rng:define[rng:element/@name]">
<xsl:variable name="element.name" select="rng:element/@name"/>
<xsl:variable name="all.attribs" select=".//rng:attribute"/>
<xsl:variable name="all.refs" select=".//rng:ref"/>
<!--
\ 'abstract': [
\ ['title', 'para'],
\ { 'os': [], 'id': [], 'vendor': [], 'xml:base': [], 'arch': [], 'condition': []}
\ ],
-->
<xsl:message>define <xsl:value-of select="$element.name"/></xsl:message>
<xsl:value-of select='concat("\ ", $element.name, ": [&#10;")'/>

<xsl:text>\ [</xsl:text>
<xsl:for-each select="$all.refs">
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:text>],&#10;</xsl:text>

<!--<xsl:text>{ </xsl:text>
<xsl:for-each select="$all.attribs">

</xsl:for-each>
<xsl:text>},&#10;</xsl:text>-->

<xsl:text>\ ],&#10;</xsl:text>
</xsl:template>

<xsl:template match="rng:*">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="a:documentation
|s:*
|rng:define[@name='suse.schema.version']
|rng:value
|rng:data/rng:param"/>
</xsl:stylesheet>