<grammar xmlns="http://www.thaiopensource.com/trex"
         ns="http://www.w3.org/1999/xhtml"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      XHTML Ruby Module in TREX
      Revision: $Id: xhtml-ruby-1.trex,v 1.2 2001/05/30 00:26:56 mimasa Exp $

      Ruby Elements

        ruby, rbc, rtc, rb, rt, rp

      This module defines patterns to support ruby annotation markup.
      This module is based on the W3C Ruby Annotation Specification:

        http://www.w3.org/TR/ruby
    </xsd:documentation>
  </xsd:annotation>

  <xsd:annotation>
    <xsd:documentation>ruby element</xsd:documentation>
  </xsd:annotation>

  <define name="ruby">
    <element name="ruby">
      <ref name="ruby.attlist"/>
      <ref name="ruby.content"/>
    </element>
  </define>

  <define name="ruby.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>rbc (ruby base component) element</xsd:documentation>
  </xsd:annotation>

  <define name="rbc">
    <element name="rbc">
      <ref name="rbc.attlist"/>
      <oneOrMore>
        <ref name="rb"/>
      </oneOrMore>
    </element>
  </define>

  <define name="rbc.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>rtc (ruby text component) element</xsd:documentation>
  </xsd:annotation>

  <define name="rtc">
    <element name="rtc">
      <ref name="rtc.attlist"/>
      <oneOrMore>
        <ref name="rt"/>
      </oneOrMore>
    </element>
  </define>

  <define name="rtc.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>rb (ruby base) element</xsd:documentation>
  </xsd:annotation>

  <define name="rb">
    <element name="rb">
      <ref name="rb.attlist"/>
      <ref name="NoRuby.content"/>
    </element>
  </define>

  <define name="rb.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>rt (ruby text) element</xsd:documentation>
  </xsd:annotation>

  <define name="rt">
    <element name="rt">
      <ref name="rt.attlist"/>
      <ref name="NoRuby.content"/>
    </element>
  </define>

  <define name="rbspan.attrib">
    <optional>
      <attribute name="rbspan">
        <data type="xsd:positiveInteger"/>
      </attribute>
    </optional>
  </define>

  <define name="rt.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>rp (ruby parenthesis) element</xsd:documentation>
  </xsd:annotation>

  <define name="rp">
    <element name="rp">
      <ref name="rp.attlist"/>
      <anyString/>
    </element>
  </define>

  <define name="rp.attlist">
    <ref name="Ruby.common.attrib"/>
  </define>

  <xsd:annotation>
    <xsd:documentation>
      Fragments for the content model of the ruby element
    </xsd:documentation>
  </xsd:annotation>

  <define name="Ruby.content.simple">
    <group>
      <ref name="rb"/>
      <ref name="rt"/>
    </group>
  </define>

  <define name="Ruby.content.simple.fallback">
    <group>
      <ref name="rb"/>
      <ref name="rp"/>
      <ref name="rt"/>
      <ref name="rp"/>
    </group>
  </define>

  <define name="Ruby.content.complex">
    <group>
      <ref name="rbc"/>
      <ref name="rtc"/>
      <optional>
        <ref name="rtc"/>
      </optional>
    </group>
  </define>

  <define name="ruby.content">
    <xsd:annotation>
      <xsd:documentation>
        Support simple ruby by default
      </xsd:documentation>
    </xsd:annotation>

    <choice>
      <ref name="Ruby.content.simple"/>
      <ref name="Ruby.content.simple.fallback"/>
    </choice>
  </define>

  <define name="Ruby.common.attrib">
    <xsd:annotation>
      <xsd:documentation>
        Ruby Common Attributes

        Content models of the rb and the rt elements are intended to
        allow other inline-level elements of its parent markup language,
        but it should not include ruby descendent elements. The following
        pattern NoRuby.content can be used to redefine those content models
        with minimum effort.
      </xsd:documentation>
    </xsd:annotation>

    <ref name="Common.attrib"/>
  </define>

  <define name="NoRuby.content">
    <xsd:annotation>
      <xsd:documentation>
        Content models of the rb and the rt elements are intended to
        allow other inline-level elements of its parent markup language,
        but it should not include ruby descendent elements. This pattern
        implements these constraints.  "Inline.model" has to be defined
        in other module.
      </xsd:documentation>
    </xsd:annotation>

    <concur>
      <ref name="Inline.model"/>
      <ref name="Ruby.concur"/>
    </concur>
  </define>

  <define name="Ruby.concur">
    <xsd:annotation>
      <xsd:documentation>Prohibit nesting of ruby</xsd:documentation>
    </xsd:annotation>

    <grammar>
      <start name="not.excluded">
        <zeroOrMore>
          <choice>
            <element>
              <not>
                <name>ruby</name>
              </not>
              <zeroOrMore>
                <attribute>
                  <anyName/>
                </attribute>
              </zeroOrMore>
              <ref name="not.excluded"/>
            </element>
            <anyString/>
          </choice>
        </zeroOrMore>
      </start>
    </grammar>
  </define>

  <xsd:annotation>
    <xsd:documentation>
      Add ruby into inline elements class; "Inline.class" has to be defined
      in other module.
    </xsd:documentation>
  </xsd:annotation>

  <define name="Ruby.class">
    <choice>
      <ref name="ruby"/>
    </choice>
  </define>

  <define name="Inline.class" combine="choice">
    <ref name="Ruby.class"/>
  </define>

</grammar>
