ludviki/tlac

Typeset lyrics with chords

v1.0 2014-06-03 10:56 UTC

README

Build Status Scrutinizer Code Quality Code Coverage

Typeset lyrics and chords by writing text, inspired by Markdown (see here and here). The lyrics and chords may be rendered as Html and/or Pdf. PDFs are created using TCPDF.

Anax-MVC users see extra description at the end.

Usage

It is recommended to use Composer to install TLAC, handling the dependency (just TCPDF) and autoloading is easy that way. Add TLAC as requirement to your composer.json (watch out for version updates!):

"require": {
    "ludviki/tlac": "1.0.*",
    ...
}

Be sure to have the autoloader loaded (require '../vendor/autoload.php'; if you don't have it already included). We assume the source is given in a text file but you can take it from a database or whatever.

Html

use ludviki\TLAC\TLAC;

$song = file_get_contents($filePath);

$tlac = new TLAC();
$html = $tlac->toHtml($song);

$html holds the rendered HTML, embed it where you want. Be sure to link to the stylesheet chords.css (found in the example/ directory.

Pdf

use ludviki\TLAC\TLAC;

$song = file_get_contents($filePath);

$tlac = new TLAC();
$pdf = $tlac->toPdf($song);

$pdf is an instance of the \TCPDF class. I refer to the documentation, for more information about how to further process the Pdf. To send it as answer to an http request you can do this (which also sends the correct headers)

$pdf->Output('tlac.pdf', 'I');

or more advanced stuff using your frameworks respons object. To save it to disk do

$pdf->Output('tlac.pdf', 'F');

Syntax

Your source file should look something like this

:title:                     
Adele - Someone like you

:verse:
{G}i heard
that you'll {D} settle down
that you'v{Em}e found a girl
and you'{C}re married now
{G}i heard
that your d{D}reams came true
...

:bridge:
i {Am}hate to turn up out o{C}f the blue uninvited
but i{Em} couldn't stay away, {D} i couldn't fight it
i'd {Am}hoped you'd see my face a{C}nd that you'd be reminded
...

Here are some explanations:

:title:                     
Adele - Someone like you

:verse:                         <= ia new block starts after an empty line
{G}i heard                      <= chords are given in curly brackets 
that you'll {D} settle down     <= space after the chord
that you'v{Em}e found a girl    <= no space after the chord
and you'{C}re married now
{G}i heard
that your d{D}reams came true
...

:bridge:                        <= another new block
i {Am}hate to turn up out o{C}f the blue uninvited
but i{Em} couldn't stay away, {D} i couldn't fight it
i'd {Am}hoped you'd see my face a{C}nd that you'd be reminded
...

If a chord i followed by a space the width of the chord is respected. that you'll {D} settle down is rendered something like this

            D
that you'll   settle down

However, and you'{C}re married now is rendered like this

        C
and you're married now

There is one additional syntax, +. It adds a fixed margin to the chord. Hej+{D}+you gets something like

      D
Hej       you

Useful for intros and outros without lyrics

{G}until the end+{em}+{D}+{G}

Integration with Anax-MVC

This is part of a hand in assignment to a course where Anax-MVC is used. To integrate into Anax-MVC update your composer.json as mentioned above.

You are now ready to load the TLAC parser as a service into the DI container in config_with_app.php somewhere after $di = new \Anax\DI\CDIFactory();

$di->set('chordparser', '\ludviki\TLAC\TLAC');

You can also integrate TLAC in more deeply into Anax by adding the following lines to \Anax\DI\CDIFactoryDefault (or if you have your own default class in this one)

$this->set('chordparser', function () {
    return new \ludviki\TLAC\TLAC();
});

Now the parser is available if the DI container is available as it should be in your controllers.

An example for a route defined by a closure (for example in webroot/index.php. Adapt this to your needs.

$app->router->add('chordparser', function() use ($app) {

    $app->theme->setTitle('Chordparser');
    $app->theme->addStylesheet('css/chords.css'); // with respect to webroot

    //
    // standard configuration: this searches for
    // ANAX_APP_PATH.'content/chordparser/text.txt'
    //
    $content = $app->fileContent->get('chordparser/text.txt');
    $text = $app->chordparser->toHtml($content);

    // nl2br filter misses in standard instalation (see pull request)
    // $source = $app->textFilter->doFilter($content, 'nl2br');
    $source = nl2br($source);


    $app->views->addString('Parsed');
    $app->views->add('default/content', [
        'content' => $text,
    ]);
    $app->views->addString('Source');
    $app->views->add('default/content', [
        'content' => $source,
    ]);
});
``