<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language
[
<!-- Replicated from "markdown.xml": -->
<!ENTITY rulerregex "\s*(?:(?:\*\s*){3,}|(?:_\s*){3,}|(?:\-\s*){3,})\s*$">
<!ENTITY indentedcodeblock "(?:\s{4}|\t).*$">
<!ENTITY listbullet "[\*\+\-]">
<!ENTITY emptyline "^\s*$">
<!ENTITY checkbox "\[[ x]\](?=\s)">
]>
<!--
  Kate highlighting module for Quarto
  (c) 2025 Thomas Friedrichsmeier (thomas.friedrichsmeier@kdemail.net)
  Heavily based on the R markdown highlighting definition, originally by:
  (c) 2014 Dirk Sarpe (dsarpe@posteo.de)

  NOTE: In fact this definition is identical to R markdown in very many parts,
        and the two should be kept in sync, except for the regions marked with
        "Quarto_specific"!

  depends on:
    Kate highlighting module for Markdown
    Kate highlighting module for R script
    Kate highlighting module for LaTeX
    Kate highlighting module for YAML
-->

<language name="Quarto"
section="Markup"
extensions="*.qmd;*.Qmd;*.QMD"
mimetype="text/x-quarto"
version="11"
kateversion="6.22"
casesensitive="true"
author="Thomas Friedrichsmeier (thomas.friedrichsmeier@kdemail.net)"
license="GPL">

  <highlighting>
    <contexts>

      <context name="Start Document" attribute="Markdown" lineEndContext="Normal Text" lineEmptyContext="Normal Text" fallthroughContext="Normal Text">
        <RegExpr String="^---$" column="0" attribute="Markdown" context="YAMLhead" beginRegion="YAMLhead block"/>
      </context>

      <context name="Normal Text" attribute="Markdown" lineEndContext="#stay" lineEmptyContext="find-code-block">
        <IncludeRules context="Common"/>
        <IncludeRules context="Overwrite Markdown Normal Text"/>
        <IncludeRules context="Normal Text##Markdown" includeAttrib="true"/>
      </context>
      <context name="find-code-block" attribute="Markdown" lineEndContext="#stay" lineEmptyContext="#stay" fallthroughContext="#pop">
        <IncludeRules context="find-code-block##Markdown" includeAttrib="true"/>
      </context>

      <context name="Common" attribute="Markdown" lineEndContext="#stay">
        <RegExpr String="```\{r.*\}" firstNonSpace="true" attribute="Structure"
                 context="R block" beginRegion="R block"/>
        <!-- BEGIN Quarto_specific: Alway require braced language sepcifier "{r}" rather than "r" at head of code blocks -->
        <RegExpr String="`\{r.*\}" attribute="Structure"
                 context="R inline"/>
        <!-- END Quarto_specific -->
        <!-- BEGIN Quarto_specific: Allow python, julia, and ojs blocks in the same was as R bloks -->
        <!-- NOTE: contrary to R, no (legacy) options seem to be allowed within the braces, for the other languages -->
        <StringDetect String="```{python}" firstNonSpace="true" attribute="Structure"
                 context="Python block" beginRegion="Python block"/>
        <StringDetect String="`{python}" attribute="Structure"
                 context="Python inline"/>
        <StringDetect String="```{julia}" firstNonSpace="true" attribute="Structure"
                 context="Julia block" beginRegion="Julia block"/>
        <StringDetect String="`{julia}" attribute="Structure"
                 context="Julia inline"/>
        <StringDetect String="```{ojs}" firstNonSpace="true" attribute="Structure"
                 context="OJS block" beginRegion="OJS block"/>
        <StringDetect String="`{ojs}" attribute="Structure"
                 context="OJS inline"/>
        <!-- END Quarto_specific -->
        <Detect2Chars char="$" char1="$" attribute="MathMode"
                      context="LaTeX equation block" beginRegion="LaTeX equation block"/>
        <DetectChar char="$" attribute="MathMode"
                    context="LaTeX inline equation"/>
        <Detect2Chars char="\" char1="$" attribute="Backslash Escape" context="#stay"/>
      </context>

      <context name="R block" attribute="Markdown" lineEndContext="#stay">
        <RegExpr String="```+(?=\s*$)" firstNonSpace="true" attribute="Structure" context="#pop"
                 endRegion="R block"/>
        <IncludeRules context="##R Script" includeAttrib="true"/>
      </context>

      <context name="R inline" attribute="Markdown" lineEndContext="#stay">
        <DetectChar char="`" attribute="Structure" context="#pop"/>
        <IncludeRules context="##R Script" includeAttrib="true"/>
      </context>

      <!-- BEGIN Quarto_specific: Allow python, julia, and ojs blocks in the same was as R bloks -->
      <context name="Python block" attribute="Markdown" lineEndContext="#stay">
        <RegExpr String="```+(?=\s*$)" firstNonSpace="true" attribute="Structure" context="#pop"
                 endRegion="Python block"/>
        <IncludeRules context="##Python" includeAttrib="true"/>
      </context>

      <context name="Python inline" attribute="Markdown" lineEndContext="#stay">
        <DetectChar char="`" attribute="Structure" context="#pop"/>
        <IncludeRules context="##Python" includeAttrib="true"/>
      </context>

      <context name="Julia block" attribute="Markdown" lineEndContext="#stay">
        <RegExpr String="```+(?=\s*$)" firstNonSpace="true" attribute="Structure" context="#pop"
                 endRegion="Julia block"/>
        <IncludeRules context="##Julia" includeAttrib="true"/>
      </context>

      <context name="Julia inline" attribute="Markdown" lineEndContext="#stay">
        <DetectChar char="`" attribute="Structure" context="#pop"/>
        <IncludeRules context="##Julia" includeAttrib="true"/>
      </context>

      <context name="OJS block" attribute="Markdown" lineEndContext="#stay">
        <RegExpr String="```+(?=\s*$)" firstNonSpace="true" attribute="Structure" context="#pop"
                 endRegion="OJS block"/>
        <IncludeRules context="Normal##JavaScript" includeAttrib="true"/>
      </context>

      <context name="OJS inline" attribute="Markdown" lineEndContext="#stay">
        <DetectChar char="`" attribute="Structure" context="#pop"/>
        <IncludeRules context="Normal##JavaScript" includeAttrib="true"/>
      </context>
      <!-- END Quarto_specific -->

      <context name="LaTeX equation block" attribute="MathMode"
               lineEndContext="#stay">
        <Detect2Chars char="$" char1="$" attribute="MathMode"
                      context="#pop" endRegion="LaTeX equation block"/>
        <IncludeRules context="MathModeDisplay##LaTeX" includeAttrib="true"/>
      </context>

      <context name="LaTeX inline equation" attribute="MathMode"
               lineEndContext="#stay">
        <DetectChar char="$" attribute="MathMode" context="#pop"/>
        <IncludeRules context="MathMode##LaTeX" includeAttrib="true"/>
      </context>

      <context name="YAMLhead" attribute="Document Headers"
               lineEndContext="#stay" fallthroughContext="Lvl0Text##YAML">
        <RegExpr String="^---$" column="0" attribute="Markdown" context="#pop"
                 endRegion="YAMLhead block"/>
        <IncludeRules context="##YAML" includeAttrib="true"/>
      </context>

      <!-- Markdown -->
      <!-- These contexts are replicated from "markdown.xml" to add the features of R Markdown. -->

      <context name="Overwrite Markdown Normal Text" attribute="Markdown" lineEndContext="#stay">
        <!-- Blockquotes -->
        <DetectChar attribute="Blockquote" context="blockquote" char="&gt;" firstNonSpace="true"/>
        <!-- Lists: avoid highlighting code blocks incorrectly, capturing indentation -->
        <RegExpr attribute="List" context="list" String="^(\s*)&listbullet;(\s+)" column="0"/>
        <RegExpr attribute="Number List" context="numlist" String="^(\s*)\d\.(\s+)" column="0"/>
        <RegExpr attribute="Number List" context="numlist2" String="^(\s*)\d\d+\.(\s+)" column="0"/>
      </context>

      <context name="list" attribute="Markdown" lineEndContext="#stay" fallthroughContext="content-list">
        <!-- Find indented code blocks, blockquotes and horizontal rules -->
        <RegExpr attribute="List: Code" String="^%1%2\s&indentedcodeblock;" column="0" dynamic="true"/>
        <RegExpr attribute="Blockquote" context="blockquote-list" String="^%1%2\s+&gt;" column="0" dynamic="true"/>
        <RegExpr attribute="List: Horizontal Rule" String="^%1%2\s+&rulerregex;" column="0" dynamic="true"/>
        <RegExpr String="&emptyline;" column="0"/>
        <!-- Text with the same indentation captured corresponds to the item list -->
        <RegExpr context="content-list" String="^%1%2\s" column="0" lookAhead="true" dynamic="true"/>
        <!-- Finish when the text has a lower indentation than the list -->
        <RegExpr context="#pop" String="^\s*\S" column="0" lookAhead="true"/>
        <!-- Highlight checkbox at the start of the item (task list) -->
        <RegExpr attribute="List: Checkbox" context="content-list" String="\s*&checkbox;"/>
      </context>
      <!-- 1. numlist (one digit) -->
      <context name="numlist" attribute="Markdown" lineEndContext="#stay" fallthroughContext="content-list">
        <RegExpr attribute="List: Code" String="^%1%2\s{2}&indentedcodeblock;" column="0" dynamic="true"/>
        <RegExpr attribute="Blockquote" context="blockquote-list" String="^%1%2\s{2,}&gt;" column="0" dynamic="true"/>
        <RegExpr attribute="List: Horizontal Rule" String="^%1%2\s{2,}&rulerregex;" column="0" dynamic="true"/>
        <RegExpr String="&emptyline;" column="0"/>
        <RegExpr context="content-list" String="^%1%2\s{2}" column="0" lookAhead="true" dynamic="true"/>
        <RegExpr context="#pop" String="^\s*\S" column="0" lookAhead="true"/>
      </context>
      <!-- 10. numlist (two or more digits) -->
      <context name="numlist2" attribute="Markdown" lineEndContext="#stay" fallthroughContext="content-list">
        <RegExpr attribute="List: Code" String="^%1%2\s{3}&indentedcodeblock;" column="0" dynamic="true"/>
        <RegExpr attribute="Blockquote" context="blockquote-list" String="^%1%2\s{3,}&gt;" column="0" dynamic="true"/>
        <RegExpr attribute="List: Horizontal Rule" String="^%1%2\s{3,}&rulerregex;" column="0" dynamic="true"/>
        <RegExpr String="&emptyline;" column="0"/>
        <RegExpr context="content-list" String="^%1%2\s{3}" column="0" lookAhead="true" dynamic="true"/>
        <RegExpr context="#pop" String="^\s*\S" column="0" lookAhead="true"/>
      </context>

      <context name="content-list" attribute="Markdown" lineEndContext="#stay" lineEmptyContext="#pop">
        <IncludeRules context="Common"/>
        <IncludeRules context="content-list##Markdown" includeAttrib="true"/>
      </context>

      <context name="blockquote" attribute="Markdown" lineEndContext="#stay" lineEmptyContext="#pop">
        <IncludeRules context="Common"/>
        <IncludeRules context="blockquote##Markdown" includeAttrib="true"/>
      </context>
      <context name="blockquote-list" attribute="Markdown" lineEndContext="#stay" lineEmptyContext="#pop">
        <IncludeRules context="Common"/>
        <IncludeRules context="blockquote-list##Markdown" includeAttrib="true"/>
      </context>

    </contexts>

    <itemDatas>
      <itemData name="Markdown" defStyleNum="dsNormal"/>
      <itemData name="Structure" defStyleNum="dsRegionMarker"/>
      <itemData name="MathMode" defStyleNum="dsRegionMarker" color="#00A000"/>
      <itemData name="Document Headers" defStyleNum="dsOthers"/>

      <itemData name="Blockquote" defStyleNum="dsAttribute" spellChecking="false"/>
      <itemData name="List" defStyleNum="dsSpecialString" bold="1" spellChecking="false"/>
      <itemData name="Number List" defStyleNum="dsSpecialString" spellChecking="false"/>
      <itemData name="List: Horizontal Rule" defStyleNum="dsNormal" bold="true" spellChecking="false"/>
      <itemData name="List: Code" defStyleNum="dsInformation"/>
      <itemData name="List: Checkbox" defStyleNum="dsVariable" spellChecking="false"/>
      <itemData name="Backslash Escape" defStyleNum="dsSpecialChar" spellChecking="false"/>
    </itemDatas>

  </highlighting>

  <general>
    <keywords additionalDeliminator="`"/>
    <comments>
      <comment name="multiLine" start="&lt;!--" end="--&gt;"/>
    </comments>
  </general>

</language>
