asciidoctor / atom-language-asciidoc

⚛ AsciiDoc language package for the Atom editor.
https://atom.io/packages/language-asciidoc
MIT License
42 stars 20 forks source link

Feature request: add syntax-highlighting support for YAML and JSON front-matter #56

Closed fusion809 closed 8 years ago

fusion809 commented 8 years ago

Hi,

I would like to request that this Atom package be given syntax-highlighting support for YAML and JSON front matter (as asciidoc can be used to write posts for Hexo, the static site generator written in JavaScript and Hexo uses YAML/JSON front matter). You could use the existing language-json and language-yaml packages to provide this capability and just set patterns to invoke these two packages, when appropriate. Like YAML front-matter for Hexo has the example format:


---
title:   "Free Operating Systems: A Comparison and Overview"
date:    2016-04-19 +1000
layout:  post

---

Thanks for your time, Brenton

mojavelinux commented 8 years ago

This is supported by Asciidoctor, so we should include it.

Is there anyway to tell the grammar to only match something at the beginning of the file? I guess we could check the language-gfm or language-markup grammars to see how it is handled there.

PR welcome!

nicorikken commented 8 years ago

That doesn't seem to hard: https://github.com/atom/language-gfm/blob/8f6dd61ea3e3254dc5429856727b3078163468d1/grammars/gfm.cson#L257-L269 Copy paste really.

ldez commented 8 years ago

It's an open block or an another mysterious block ?

nicorikken commented 8 years ago

The Asciidoctor user manual seems to be somewhat hesitant in defining the proper grammar, using 'typically' and 'e.g.':

Many static site generators (i.e., Jekyll, Middleman, Awestruct) rely on "front matter" added to the top of the document to determine how to render the content. Front matter typically starts on the first line of a file and is bounded by block delimiters (e.g., ---).

Copying the GFM solution probably covers more than 99% of the cases 😉

nicorikken commented 8 years ago

3 dashes, so no open block

mojavelinux commented 8 years ago

Correct, it is not an open block. In fact, it's not really an AsciiDoc block at all. It is a block that is skimmed off of the top of the document before the AsciiDoc processor sees the source (i.e., an external preprocessor).

It does look a lot like an open block, but with 3 dashes. The content is YAML (or JSON, though most implementations I've seen are YAML).

The only trick to getting this right is that it must be only permitted at the top of the document. That means in the top-level grammar, we need to look for it before blocks. It might even be necessary to add another level of nesting so that we look for "front matter" then "document" where "document" is the current grammar.

nicorikken commented 8 years ago

Can we use the \A selector like GFM does, to only match at the beginning of the document? Still conflicts should be avoided. If I find the time I can prototype.

ldez commented 8 years ago

\Amatch the beginning of string. It's linked to multi-lines support, no matchers for position in the document.

If we do this feature, we can capture only a block beginning and ending with ---

mojavelinux commented 8 years ago

Actually, if you use begin, then \\A does seem to match the beginning of the document only. I tried a sample Markdown document that uses the gfm grammar. If you move the front matter block down by one line, it stops highlighting it.

Here's how it is defined there:

  {
    'begin': '\\A---$'
    'end': '^(---|\\.\\.\\.)$'
    'captures':
      '0':
        'name': 'comment.hr.gfm'
    'name': 'front-matter.yaml.gfm'
    'patterns': [
      {
        'include': 'source.yaml'
      }
    ]
  }

(I don't know why end also include three dots. We should only use ^---$).

We should use the same names as we do for a source block of type YAML.

mojavelinux commented 8 years ago

Basically, front matter is a special form of:

[source, yaml]
----
name: value
----

Except it uses three instead of four delimiters, no attribute list and must be at the start of the file.

ldez commented 8 years ago

conflicts with horizontal rules...

mojavelinux commented 8 years ago

Yes, but the match should win if it is listed first. No?

ldez commented 8 years ago

Tests saves my life! :heart:

See #106

mojavelinux commented 8 years ago

I :heart: to :ear: that!

nicorikken commented 8 years ago

The work of @ldez just got merged, to be released in the upcoming release.

nicorikken commented 8 years ago

@fusion809 Can you check back in now, or after the release, to see if this works for your use-case?

fusion809 commented 8 years ago

Just gave it a go and it's doing as intended thanks.