
Description = "reStructured Text"

Categories = {"markup"}

Keywords={
  -- URLs
  { Id=3,
    Regex=[[ https?\:\/\/[^\s`]+ ]],
  },
  -- tables
  { Id=4,
    Regex=[[ ^\+[\-=]{2,}\S+|^[=]{3,}\s[=\s]+|^[\-]{3,}\s[-\s]+]],
  },
  --headers
  --needs to be placed before kwd 4
  { Id=1,
    Regex=[[ ^[=]{3,}(?!\s)|^[\-]{3,}(?!\s)|^[`]{3,}|^[:]{3,}|^[']{3,}|^["]{3,}|^[~]{3,}|^[_]{3,}|^[\*]{3,}|^[\+]{3,}|^[<]{3,}|^[>]{3,} ]],
  },
  --emphasizers
  { Id=4,
    Regex=[[ ^\s*(\-\s|>|[\d\#]+?\.) ]],
  },
  { Id=4,
    Regex=[[ \*{1,2}.*?\*{1,2} ]],
  },
  { Id=4,
    Regex=[[ \:.*?\: ]],
  },
  --lists
  { Id=5,
    Regex=[[ ^(\:.*?:|^\/\w|^\-{1,2}[\S]+) ]],
  },
  { Id=5,
    Regex=[=[ (\[[\w#\*]+?\])? ]=],
  },
  --references
  { Id=5,
    Regex=[[ ^(\.\.\s+?(\_?\w+?\:\:?)?) ]],
  },
  --directives
  { Id=4,
    Regex=[[ \|.*?\||\| ]],
  },
--directives
  { Id=666,
    Regex=[[ \( ]],
  },
}

Strings={
  Delimiter=[[```|``|`]]
}

IgnoreCase=false

Comments = {
  {
    Block=false,
    Delimiter = { [[^\.\.(?!.+[\:\[])]] }
  }
}

commentIdent=0
commentLine=0
initComment=0

function OnStateChange(oldState, newState, token, kwgroup, lineNumber, column)

  -- add somewhat whacky logic to support comments based on indentation
  -- empty lines end comments, as do smaller indentations than the initial

  if newState==HL_LINE_COMMENT  then
        commentLine = lineNumber
        commentIdent = column
        initComment=1
  end

  if oldState==HL_LINE_COMMENT then
    if (commentLine ==0 and commentIdent==0) or initComment==1 then
        commentLine = lineNumber
        initComment=0
    end
  end

  if lineNumber == 1+commentLine and commentLine>0 then
        commentLine = lineNumber
        if (column >= commentIdent ) then
            return HL_LINE_COMMENT
        else
            commentIdent = 0
            commentLine = 0
            return HL_REJECT
        end
    end

  if newState==HL_KEYWORD and kwgroup==1 and token[0]~='#' and lineNumber>1 then
    AddPersistentState(lineNumber-1, kwgroup, column, #token)
  end

  if newState==HL_ESC_SEQ then
    return HL_REJECT
  end
  if newState==HL_KEYWORD and kwgroup==666 then
    return HL_REJECT
  end
end
