GithubHelp home page GithubHelp logo

make-html's Introduction

MakeHTML

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.

make-html's People

Contributors

itssamtaylor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.