songleaf.chordpro

Parse ChordPro text into a Song.

ChordPro puts chords inline, in square brackets, at the syllable they land on (Paper [G]boats [D]drift), and describes the song with {directive: value} lines.

  • [X] becomes a chord annotation at that character offset of the bracket-free lyric line; a line of chords only becomes an empty text line carrying its chords in order, as in songleaf.parse.

  • {title}, {artist}, {key}, {capo}, {tempo} (and their short forms) become metadata.

  • {soc}/{eoc} (and verse, bridge, tab-less variants) and {c: label} comments become section annotations; a section with no lines of its own is a marker ({"marker": True}), as in songleaf.parse.

  • {x_songleaf_before: 0, 3} (written by songleaf.export.to_chordpro()) marks the chords at those positions, counted over the song’s chords in text order, as sounding before their syllable (timing="before"): ChordPro has no way to say it.

  • # comments and other unknown directives are dropped.

>>> song = parse_chordpro("{title: Paper Boats}\n{soc}\nPaper [G]boats [D]drift\n{eoc}")
>>> song.text
'Paper boats drift'
>>> [(a.start, a.body["symbol"]) for a in song.of_kind("chord")]
[(6, 'G'), (12, 'D')]
>>> song.meta["title"], song.of_kind("section")[0].body["label"]
('Paper Boats', 'Chorus')

Module Attributes

BEFORE_DIRECTIVE

The namespaced directive carrying timing="before" chords; apps that don't know it skip it.

Functions

parse_chordpro(raw, *[, meta, provenance])

Parse ChordPro text into a Song.

songleaf.chordpro.BEFORE_DIRECTIVE = 'x_songleaf_before'

The namespaced directive carrying timing="before" chords; apps that don’t know it skip it.

songleaf.chordpro.parse_chordpro(raw, *, meta=None, provenance=None)[source]

Parse ChordPro text into a Song.

Parameters:
  • raw (str) – The ChordPro text.

  • meta (Mapping | None) – Metadata to start from; directives in the text override it.

  • provenance (Mapping | None) – Where the text came from, stored as is.

Return type:

Song