<?xml version="1.0" encoding="UTF-8"?>
<!--

 This file is part of GtkSourceView

 Author: Guillaume Chéreau <guillaume.chereau@gmail.com>

 GtkSourceView is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 GtkSourceView is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, see <http://www.gnu.org/licenses/>.

-->
<language id="rst" name="reStructuredText" version="2.0" _section="Markup">
  <metadata>
    <property name="mimetypes">text/x-rst</property>
    <property name="globs">*.rst</property>
    <property name="line-comment-start">..\u0020</property>
  </metadata>

  <styles>
    <style id="header" name="Header" map-to="def:heading"/>
    <style id="emphasis" name="Emphasis" map-to="def:emphasis"/>
    <style id="strong-emphasis" name="Strong Emphasis" map-to="def:strong-emphasis"/>
    <style id="bullet" name="Bullet" map-to="def:list-marker"/>
    <style id="lineblock" name="Line block" map-to="def:string"/>
    <style id="directive" name="Directive" map-to="def:constant"/>
    <style id="comment" name="Comment" map-to="def:comment"/>
    <style id="interpreted" name="Interpreted" map-to="def:identifier"/>
    <style id="link" name="Link" map-to="def:link-text"/>
    <style id="substitution" name="Substitution" map-to="def:type"/>
    <style id="code" name="Code" map-to="def:inline-code"/>
    <style id="url" name="Url" map-to="def:net-address"/>
  </styles>

  <definitions>

    <context id="directive" class="no-spell-check" style-ref="directive">
      <start>^(\s*)\.\. \w*::</start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
    </context>

    <context id="footnotes" style-ref="directive">
      <start>^(\s*)\.\. \[([0-9]+|\#|\#\w+|\*)\] </start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
    </context>

    <context id="citation" style-ref="directive">
      <start>^(\s*)\.\. \[\w+\] </start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
    </context>

    <context id="link-target" class="no-spell-check" style-ref="directive">
      <start>^(\s*)\.\. _.*:</start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
    </context>

    <context id="comment" style-ref="comment">
      <start>^(\s*)\.\.\s*.+$</start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
    </context>

    <context id="comment-single" style-ref="comment">
      <start>^(\s*)\.\.\s*$</start>
      <end>^(?!(\%{1@start}   )|(\s*$))</end>
      <include>
        <context id="comment-non-empty-block" extend-parent="false" end-parent="true">
          <start>^(\s*)\S</start>
          <end>^(?!(\%{1@start})|(\s*$))</end>
        </context>
        <context id="comment-empty-block" extend-parent="false" end-parent="true">
          <match>^\s*$</match>
        </context>
      </include>
    </context>

    <context id="literal-block">
      <include>
        <context id="literal-indented" style-ref="code">
          <start>^(\s*)::\s*$</start>
          <end>^(?!(\%{1@start}   )|(\s*$))</end>
        </context>
        <context id="literal-quoted" style-ref="code">
          <start>(?&lt;!:)::\s*$</start>
          <include>
            <context id="literal-quoted-block" end-parent="true">
              <start>^(\s*)\S</start>
              <end>^(?!(\%{1@start})|(\s*$))</end>
            </context>
          </include>
        </context>
      </include>
    </context>

    <context id="title-adornment" style-ref="header">
      <match>^\s*(=+|\-+|`+|:+|(\.\.\.+)|'+|"+|~+|\^+|_+|\*+|\++|#+)\s*$</match>
    </context>

    <context id="bullet" style-ref="bullet">
      <match>^\s*[\*\+\-•‣⁃] </match>
    </context>

    <define-regex id="roman-upper" extended="true">
      (M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3}))
    </define-regex>

    <define-regex id="roman-lower" extended="true">
      (m*(c[md]|d?c{0,3})(x[cl]|l?x{0,3})(i[xc]|c?i{0,3}))
    </define-regex>

    <define-regex id="roman-numerals" extended="true">
      ((\%{roman-upper})|(\%{roman-lower}))
    </define-regex>

    <define-regex id="enumerator-chars" extended="true">
      ([#]|[a-zA-Z]|([0-9]+)|(\%{roman-numerals}))
    </define-regex>

    <context id="enumerator" style-ref="bullet">
      <match>^\s*((\%{enumerator-chars}\.)|(\(?\%{enumerator-chars}\)) )</match>
    </context>

    <context id="lineblock" style-ref="lineblock">
      <start>^(\s*)\| \S</start>
      <end>^(?!(\%{1@start}(  |\|))|(\s*$))</end>
    </context>

    <context id="field" style-ref="bullet">
      <match>:(?![: ])([^:\\]|\\.|:(?!([ `]|$)))*(?&lt;! ):( +|$)</match>
    </context>

    <define-regex id="legal-start" extended="true">
      ((?&lt;=(\s|[-:/'"&lt;([{‘’“”‚„]))|^)
    </define-regex>

    <define-regex id="legal-end" extended="true">
      ((?=(\s|[-.,:;!?\\/'")\]}&gt;‘’“”‚„]))|$)
    </define-regex>

    <context id="strong-emphasis" style-ref="strong-emphasis">
      <start>(\%{legal-start})\*\*(?!(\s|$))</start>
      <end>(?&lt;!\s)\*\*(\%{legal-end})</end>
    </context>

    <context id="emphasis" style-ref="emphasis">
      <start>(\%{legal-start})\*(?!(\s|$))</start>
      <end>(?&lt;!\s)\*(\%{legal-end})</end>
    </context>

    <context id="literals">
      <include>
        <context id="inline-literals" class="no-spell-check" style-ref="code">
          <start>(\%{legal-start})``(?!(\s|$))</start>
          <end>(?&lt;!\s)``(\%{legal-end})</end>
        </context>
      </include>
    </context>

    <context id="target" style-ref="link">
      <start>(\%{legal-start})_`(?!(\s|$))</start>
      <end>(?&lt;!\s)`(\%{legal-end})</end>
    </context>

    <context id="interpreted" style-ref="interpreted">
      <start>(\%{legal-start}):[\w:]+:`(?!(\s|$))</start>
      <end>(?&lt;!\s)`(\%{legal-end})</end>
    </context>

    <!-- FIXME: Can we separate these two somehow? -->
    <context id="interpreted-or-link" style-ref="link">
      <start>(\%{legal-start})`(?!(\s|$))</start>
      <end>(?&lt;!\s)`(:[\w:]+:)?_?_?(\%{legal-end})</end>
    </context>

    <context id="simple-link" style-ref="link">
      <match>(\%{legal-start})[^\W_]\w*_(\%{legal-end})</match>
    </context>

    <context id="substitution" style-ref="substitution">
      <start>(\%{legal-start})\|(?!(\s|$))</start>
      <end>(?&lt;!\s)\|_?_?(\%{legal-end})</end>
    </context>

    <context id="url" style-ref="url">
      <match>((http)|(file))://\S*</match>
    </context>

    <context id="rst">
      <include>
        <context ref="directive"/>
        <context ref="link-target"/>
        <context ref="footnotes"/>
        <context ref="citation"/>
        <context ref="comment"/>
        <context ref="comment-single"/>
        <context ref="literal-block"/>
        <context ref="title-adornment"/>
        <context ref="bullet"/>
        <context ref="enumerator"/>
        <context ref="lineblock"/>
        <context ref="field"/>
        <context ref="strong-emphasis"/>
        <context ref="emphasis"/>
        <context ref="literals"/>
        <context ref="target"/>
        <context ref="interpreted"/>
        <context ref="interpreted-or-link"/>
        <context ref="simple-link"/>
        <context ref="substitution"/>
        <context ref="url"/>
      </include>
    </context>
  </definitions>

</language>
