2

In this question, I've inquired about how to highlight more than one markdown list punctuation item. I now want to extend the highlighting to roman numbers as well, because those are supported by Pandoc's extensions, which I use to create PDFs from markdown source.

In the Markdown Extended syntax definition, in line 1180, I've inserted the following regex:

^\s{0,4}([0-9]+|[ivxlcdm]+|[IVXLCDM]+)\.(?=\s\w)

Now it looks like this:

list-paragraph:
- match: \G\s+(?=\S)
    push:
        - meta_scope: meta.paragraph.list.markdown
        - match: ^\s*$
            pop: true
        - match: '^\s{0,4}([*+-])(?=\s)'
            scope: punctuation.definition.list_item.markdown
        - match: '^\s{0,4}([0-9]+|[ivxlcdm]+|[IVXLCDM]+)\.(?=\s\w)'
            captures:
                1: punctuation.definition.list_item.markdown punctuation.definition.list_item.number.markdown
                2: punctuation.definition.list_item.markdown
        - include: inline

I understand, that this doesn't take validity of the roman numbers into account, but I'm going to type them correctly anyway, so I con't care i it highlights other invalid roman numbers as well. Also once a list in markdown starts with a valid number, usually i., the numbering in automatically calculated by Pandoc, when it creates the PDF, so that one could type i. 10 times, and it would still be roman numbers from 1 to 10 in the PDF.

I checked this regular expression with the Online regex tester and debugger. The mode I used is mg, because I read the following on the SublimeText websites:

Regexes are only ever run against a single line of text at a time.

g makes the matching going on after finding a match. m makes the matching consider ^ as beginning of the line and $ as the end of the line.

This is what I understand SublimeText does internally.

My test text is the following:

## Normal Equation

When the number of features for an $x^{(i)}$ of the training data is not too high, maybe lower than $9000$, an alternative way to [gradient descent](#gradient-descent-algorithm) for solving the optimization problem of the [cost function](#cost-function), using the normal equation, is feasible.

The vector $\theta$, which contains the coefficients for the hypothesis function can be optimized in one step using the following formula:

$$\theta = (X^T X)^{-1} X^T y$$

Where $X$ is a matrix, is constructed as follows:


+as
+ as
-as
-asa
* asas
* asas

asas

1. 
2. 1212
3. 1212

qqq

I. asas
II. asa
III. asa

qqq

i. sa
ii. 1212
iii. asas

asdasd *asasas* 1. sadqwqe. *This is fat text!* **double** ewwrew ass a as as asa aas  asasasas  1. ewr34 43543

The test of the regex is completely successful, it highlights exactly as I want it to. However, when I paste it into the syntax definition of Markdown Extended, roman numbers stay white, are not highlighted.

Example screenshot:

screenshot of sublimetext not highlighting roman numbers

So I don't know what's wrong with the regex. How do I need to change it, to also include roman numbers (not necessarily correct, valid roman numbers)?

Additional Info

  • SublimeText version: 3103
  • OS: Xubuntu 14.04

1 Answer 1

2

The regex itself works fine, but is never triggered in numbered lists. You also have to adapt the regex in line 693 to '^[ ]{0,3}([0-9]+|[ivxlcdm]+|[IVXLCDM]+)(\.)(?=\s)'. In this line the list-paragraph context is pushed. However you should be aware, that this will also be highlighted (and is already highlighted):

1. ...
ii. ...

If want to avoid this you could add a context list-paragraph-roman and push that for roman numbers.

4
  • Wow, thanks, worked right away. By these answers I learn more any more about how this yaml file actually works, or how it is used by Sublime Text. I find the documentation a bit difficult to understand and to be unclear. Mar 12, 2016 at 12:57
  • @Zelphir I think this documentation is well written and very helpful.
    – r-stein
    Mar 12, 2016 at 17:01
  • Maybe when you're into the matter, but that's the one I had problems with. For example: What is that stack which is pushed to? When does Sublime Text pop elements? Only when I writ pop somewhere? What is an inner or anonymous context? Maybe these are all well known terms for people who work with syntax parsing a lot, but for me it's a bit too little explanation : ) Mar 12, 2016 at 17:06
  • 1
    As far as I understand it the stack consists of contexts and only the top (last pushed) context is matched against the code. However the other elements can add meta_scopes to get a better scoping. Contexts are only poped, when you write pop. Therefore I would recommend to always directly write the match statement, to pop the context, inside the push. An inline, anonymous context is just if you directly write the matches behind the push instead of give them a new context name.
    – r-stein
    Mar 12, 2016 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .