Hello,

Pygments is a Python syntax highlighter supporting many
languages, used by Sphinx-documented projects, users of
the 'minted' LaTeX package, Wikipedia, and various websites.
I'd like to see Pygments supporting LilyPond. Here is my
pull request (the equivalent of a merge request, on GitHub):

https://github.com/pygments/pygments/pull/1845

(Note that there is a previous attempt,
https://github.com/pygments/pygments/pull/1371. However,
it is far less precise, does not understand Scheme, and,
above all, reuses code covered by the GPL from Frescobaldi,
making it unsuitable for inclusion in the BSD-licensed
Pygments.)

Reviews and testing would be appreciated. In particular,
if you would not like to have a later unpleasant surprise
with my aesthetic choices, please give the style a look;
the eventual goal would be to make this used in our
documentation. The setup to start testing is
(assuming Python 3 installed):

git clone https://github.com/Jean-Abou-Samra/pygments.git
cd pygments
git checkout lilypond
python3 -m venv pygments-env
source pygments-env/bin/activate
pip install -r requirements.txt

Then you can highlight file.ly, outputting to file.html,
using

python -m pygments -f html -O full,style=lilypond file.ly > file.html

(If you begin a new session of testing, don't forget
to start with 'source pygments-env/bin/activate'.)

To whet your appetite, a highlighted version of the test
file included in the PR is attached.

Cheers,
Jean
\version "2.23.3"

% This is a test file for Pygments' LilyPond support. To compile
% it with Guile 1 (as in official releases as of this writing),
% remove the "commented form" below.

%{
  All supported constructs are covered in
  this file.  For example, multi-line comments.

  These are non-nested. The following will
  end the whole comment:
  %{ %}

\header {
  title = "This is an assignment to a string."
  composer = \markup \smallCaps "And this is a markup"
}
% The following is just a comment.
%}

\paper {
  indent = 30\staff-space
  page-count = 1
}

myFunc =
# #;(This is a commented form. After it, we are still in Scheme mode.)
(define-music-function (music n) (ly:music? index?)
   (let* ((repeated (make-list n music))
          (copied (map ly:music-deep-copy repeated)))
     ; This is a Scheme comment.
     (make-sequential-music copied)))

mySecondFunc =
#(define-music-function (music) (ly:music?)
   #{
     % LilyPond syntax here.
     \compressMMRests
       \shiftDurations -2 1
        # ; Scheme syntax again.
         (ly:music-deep-copy music)
   #})

myPitch = ##{ c #}

% Here we should be back to LilyPond mode. This
% is a LilyPond comment.

<<
  \new Staff \with {
    \consists Duration_line_engraver
  }
  \relative c' {
    \clef alto
    \time 6/8
    \key d \major
    \cadenzaOn
    deses'!4.~(\tweak thickness 4\( deses^\p-\signumcongruentiae_1\4
    deses\longa) \myFunc { r } 4 des8 8[ <des ges>8]\)
    \bar "||"
    \cadenzaOff
    \once \hide NoteHead
    \once \override NoteHead.no-ledgers = ##t
    \once \omit Dots
    \once \override Staff.DurationLine.thickness = #5
    c''?2.:16\-^"Some music" |
    \mySecondFunc
      R1*1/2^\markup \center-column {
               Some
               Text
               In
               A
               \bold \italic Column!
               super % not highlighted as a markup command
             }
    \repeat unfold 4 { c8\< c^\> c\p\! }
  }
  \addlyrics {
    \set Score.melismaBusyProperties = #'()
    My Lily -- Song
  }
  \chordmode {
    c cis:3+ des:maj7/+e
  }
  \new TabVoice {
    f'4\^ g'4\^ f'2
  }
  \drums {
    hihat4 hh bassdrum bd
  }
>>
\version "2.23.3"

% This is a test file for Pygments' LilyPond support. To compile
% it with Guile 1 (as in official releases as of this writing),
% remove the "commented form" below.

%{
  All supported constructs are covered in
  this file.  For example, multi-line comments.

  These are non-nested. The following will
  end the whole comment:
  %{ %}

\header {
  title = "This is an assignment to a string."
  composer = \markup \smallCaps "And this is a markup"
}
% The following is just a comment.
%}

\paper {
  indent = 30\staff-space
  page-count = 1
}

myFunc =
# #;(This is a commented form. After it, we are still in Scheme mode.)
(define-music-function (music n) (ly:music? index?)
   (let* ((repeated (make-list n music))
          (copied (map ly:music-deep-copy repeated)))
     ; This is a Scheme comment.
     (make-sequential-music copied)))

mySecondFunc =
#(define-music-function (music) (ly:music?)
   #{
     % LilyPond syntax here.
     \compressMMRests
       \shiftDurations -2 1
        # ; Scheme syntax again.
         (ly:music-deep-copy music)
   #})

myPitch = ##{ c #}

% Here we should be back to LilyPond mode. This
% is a LilyPond comment.

<<
  \new Staff \with {
    \consists Duration_line_engraver
  }
  \relative c' {
    \clef alto
    \time 6/8
    \key d \major
    \cadenzaOn
    deses'!4.~(\tweak thickness 4\( deses^\p-\signumcongruentiae_1\4
    deses\longa) \myFunc { r } 4 des8 8[ <des ges>8]\)
    \bar "||"
    \cadenzaOff
    \once \hide NoteHead
    \once \override NoteHead.no-ledgers = ##t
    \once \omit Dots
    \once \override Staff.DurationLine.thickness = #5
    c''?2.:16\-^"Some music" |
    \mySecondFunc
      R1*1/2^\markup \center-column {
               Some
               Text
               In
               A
               \bold \italic Column!
               super % not highlighted as a markup command
             }
    \repeat unfold 4 { c8\< c^\> c\p\! }
  }
  \addlyrics {
    \set Score.melismaBusyProperties = #'()
    My Lily -- Song
  }
  \chordmode {
    c cis:3+ des:maj7/+e
  }
  \new TabVoice {
    f'4\^ g'4\^ f'2
  }
  \drums {
    hihat4 hh bassdrum bd
  }
>>

Reply via email to