taylornetwork/make-html

There is no license information available for the latest version (1.6.1) of this package.

A trait for to convert plain text to html, inlcuding links.

1.6.1 2018-10-23 03:12 UTC

This package is auto-updated.

Last update: 2024-11-18 17:23:32 UTC


README

This is a class and trait for Laravel that will convert text with line breaks and links into HTML. It also adds simple HTML tag generation as well as a helper function for imploding associative arrays.

Install

Via Composer

$ composer require taylornetwork/make-html

Dependencies

TaylorNetwork\MakeHTML will install TaylorNetwork\LaravelHelpers. Laravel should auto discover that package, but if you run into problems you may need to include the provider manually.

Setup

This package is set up with auto discovery so you should be good to go out of the box. Check Manual Setup below if something isn't working.

Manual Setup

If auto discovery doesn't work...

Add the service provider to the providers array in config/app.php.

'providers' => [
    TaylorNetwork\MakeHTML\MakeHTMLServiceProvider::class,
];

Publishing

Run

php artisan vendor:publish

This will add makehtml.php to your config directory.

Usage

You can use this package either by including the trait in a class you want to implement the functionality or by creating a new instance of the class.

Trait

Import the trait into your class.

use TaylorNetwork\MakeHTML\MakeHTML;

class DummyClass
{
    use MakeHTML;

    // Code
}

The MakeHTML trait includes two functions, usage examples below.

makeHTML (string $text)

Accepts one parameter, the text you want the generator to parse.

Given this example variable $text

$text = 'Example text
with line
breaks. And a
link: http://example.com/page/1/2?q=This&that=other';

Calling makeHTML

$this->makeHTML($text);

Returns

'Example text<br>with line<br>breaks. And a<br>link: <a href="http://example.com/page/1/2?q=This&that=other">example.com</a>'

getHTMLGeneratorInstance ()

Accepts no parameters and returns an instance of HTMLGenerator class, or creates one.

$this->getHTMLGeneratorInstance();

You can chain any methods from the HTMLGenerator class onto that to return functionality.

See class usage below for examples.

Class

Instantiate the class.

use TaylorNetwork\MakeHTML\HTMLGenerator;

$instance = new HTMLGenerator();

Available Methods

makeLinks (string $text)

Converts any found links in the string to clickable links with <a> tag.

Will take the base URL as the caption for the link.

For example:

$textWithLink = 'I have a link http://example.com/page/1/2/3?query=string';

Call makeLinks($textWithLink)

$instance->makeLinks($textWithLink);

Returns

'I have a link <a href="http://example.com/page/1/2/3?query=string">example.com</a>'
convertLineEndings (string $text)

Converts all line endings to HTML <br> tags.

$textWithLineBreaks = 'This
is
text
with
line
breaks.';

Call convertLineEndings($textWithLineBreaks)

$instance->convertLineEndings($textWithLineBreaks);

Returns

'This<br>is<br>text<br>with<br>line<br>breaks.'
makeHTML (string $text)

Calls convertLineEndings($text) and makeLinks($text) and returns the converted text.

generateTag (string $tag, array $attributes, boolean $closeTag = true)

Will generate an HTML tag with any attributes given.

To generate a <div> tag with no attributes

$instance->generateTag('div', []);

Returns

'<div></div>'

To generate a <div> tag with attributes

$attributes = [ 'class' => 'example-class second-class third' 'data-attr' => 'value' ];

Call function

$instance->generateTag('div', $attributes);

Returns

'<div class="example-class second-class third" data-attr="value"></div>'

To generate a <div> tag with attributes and also data between the tags, add the external attribute and everything there will be added between the tags.

$attributes = [
    'class' => 'example-class',
    'id' => 'div1',
    'name' => 'some-name',
    'external' => 'This is external data!',
];

Call function

$instance->generateTag('div', $attributes);

Returns

'<div class="example-class" id="div1" name="some-name">This is external data!</div>'

To generate a <div> tag and keep it open, to process more external data for example.

$instance->generateTag('div', $attributes, false);

Returns

'<div class="example-class" id="div1" name="some-name">This is external data!'

closeTag(string $tag)

Closes a given tag

$instance->closeTag('div');

Returns

'</div>'

Magic Method

You can also call generateTag($tag, $attributes) by calling the tag you want followed by Tag

To generate a <div> tag this way

$instance->divTag($attributes);

Would call generateTag('div', $attributes) for you.

Config

The config file once published is in config/makehtml.php

Credits

License

The MIT License (MIT). Please see License File for more information.