Sublime Forum

Cloze formatting

#1

The default XML colour formatting is great for regular XML files but could be improved for Moodle Cloze format.

In particular, i would like to have a colour change for {1:MULTICHOICE_H: and tilde (~) and all perhaps all < >HTML tags.

After much research, I can’t find out how to create a new package to achieve this.

Any advice will be very welcome.

0 Likes

#2

Moodle Cloze format.

Where is this Moodle Cloze syntax defined formally?

i would like to have a colour change for {1:MULTICHOICE_H: and tilde (~) and all perhaps all < >HTML tags.

What you could do is write a syntax def that by default uses XML but uses a with_prototype context for your Cloze needs. The Jinja example is probably closest to what you’re wanting to start out with, see the section “Including Other Files”. If you’re writing a lot of these kind of files, it might be worth the trouble of learning how to write Sublime syntax def files.

0 Likes

#3

Thanks, I’ll check that out.

Defined here:

https://docs.moodle.org/22/en/Embedded_Answers_(Cloze)_question_type

Section:
Importing CLOZE questions

Multiple CLOZE questions can be imported using the XML format:

0 Likes

#4

I’m guessing you want something like this:

This is for the bare Cloze syntax. The syntax I used is:

%YAML 1.2
---
name: Cloze
file_extensions: [cloze]
scope: text.cloze
variables:
  Identifier: '[[:alpha:]][[:alpha:]_]*'
contexts:
  main:
    - match: \{
      scope: punctuation.section.braces.begin.cloze
      push:
        - meta_scope: meta.question.cloze
        - match: \}
          scope: punctuation.section.braces.end.cloze
          pop: true
        - match: (\%)(\d+)(\%)
          captures:
            1: variable.language.cloze
            2: constant.numeric.cloze
            3: variable.language.cloze
        - match: (\d+)?(:)({{Identifier}})(:)
          captures:
            1: constant.numeric.cloze
            2: punctuation.separator.cloze
            3: entity.other.attribute-name.cloze
            4: punctuation.separator.cloze
        - match: \#
          scope: punctuation.definition.comment.cloze
          push:
            - meta_scope: comment.block.cloze
            - match: (?=[=~}])
              pop: true
        - match: \~
          scope: punctuation.separator.cloze
        - match: =
          scope: keyword.operator.assignment.cloze

Save this in Packages/User/Cloze.sublime-syntax

Now to embed this into XML, the easiest hack is to use the with_prototype trick:

%YAML 1.2
---
name: XML (Cloze)
scope: text.xml
file_extensions: [cloze, xml]

contexts:
  main:
    - match: ""
      push: scope:text.xml
      with_prototype:
        - match: '<!\[CDATA\['
          scope: punctuation.definition.string.begin.xml
          push:
            - meta_scope: meta.group.cloze
            - match: ']]>'
              scope: punctuation.definition.string.end.xml
              pop: true
            - include: scope:text.cloze

Save this in Packages/User/XML (Cloze).sublime-syntax

This will result in:

Good luck!

1 Like

#6

I’ve been unable to get to this for a while but it looks perfect. thanks a million.
However, I didn’t get quite the colouring as you. In particular the ‘<![CDATA[’. ‘[[’ and tildes are not highlighted.
Is this simply because I used the Monocai colour scheme ( Tried a few other too)?

Just found ‘neon’ scheme which fixed the CDATA highlight but still not the others

Thanks,
Stuart.

1 Like

#7

I assigned “punctuation” to some small elements like the tilde ~ and the brackets {} and the colon following the question type. It depends on the color scheme if “punctuation” gets highlighted. In my screenshot I used the “Boxy Monokai” color scheme as that highlights punctuation. That theme is part of the “Boxy” package.

0 Likes

#8

I reinstalled everything and followed your instructions.
You should post this as a theme.

Love it. Many thanks mate.

2 Likes

#9

Again thanks Raoul.

I figured most of this out, finally, and made some small changes, but just one small thing I cant figure out:

I copied the “- include: scope:text.cloze” after the first scope line to get the formatting in the table html in the below code
(this was not in the original example)…

and all is well except the apparent error highlight on “]]>”. shown below
.
,

Any idea how to fix this?
Regards,
Stuart.

1 Like

#10

Hi Stuart,

When you match the characters <!\[CDATA\[, you want to “push” a new syntax context onto the syntax stack that handles the Cloze syntax. You’ll have to move the - include: scope:text.cloze either after the push or before the - match: ...

I haven’t looked into it much, but I think it’s actually better to remove that - include: scope:text.cloze line (the first one), and embed the HTML syntax in the Cloze syntax instead.

1 Like

#11

If you post your syntax as text to copy and paste, it will be easier for others to suggest an alternative.

3 Likes