# Parser

{% hint style="danger" %}
**Please note:** This page is under construction and has not been finished yet.
{% endhint %}

## Introduction

Use the following to retrieve the parser class object.&#x20;

```php
$tp = e107::getParser();
```

## Parser methods

### toHTML()

Parse HTML in various ways. eg. replace constants, convert bbcode etc.

```php
$tp->toHTML($text, $parseBB = false, $modifiers = '', $postID = '', $wrap = false)
```

<table><thead><tr><th width="150">Parameter</th><th width="150">Type</th><th width="297.6130768997373">Description</th><th width="150">Mandatory?</th></tr></thead><tbody><tr><td><strong>text</strong></td><td>string</td><td>text or HTML to be parsed</td><td><strong>Yes</strong></td></tr><tr><td>bparseBB</td><td>boolean</td><td>set to true to parse BBcodes into HTML</td><td>No</td></tr><tr><td>modifiers</td><td>string</td><td>Choose from pre-defined <a href="#parser-modifiers">Parser modifiers</a>. </td><td>No</td></tr><tr><td>postID</td><td></td><td></td><td>No</td></tr><tr><td>wrap</td><td>boolean</td><td><br><em>Default: false</em></td><td>No</td></tr></tbody></table>

#### Example

```php
$tp->toHtml("<strong class="bbcode bold bbcode-b bbcode-b-page">Bold print</strong>", true, 'BODY'); 
```

### toDate()

Convert a UNIX timestamp into a readable format.

```php
$tp->toDate($datestamp = null, $format = 'short')
```

<table><thead><tr><th width="150">Parameter</th><th width="150">Type</th><th width="242.2520274269957">Description</th><th>Mandatory?</th></tr></thead><tbody><tr><td><strong>datestamp</strong></td><td>unix <br>timestamp</td><td></td><td><strong>Yes</strong></td></tr><tr><td>format</td><td>string</td><td>short - Short date format as defined in admin preferences<br><br>long - Long date format as defined in admin preferences<br><br>relative - relative time format. eg. "2 days ago"<br><br><em>Default: short</em></td><td></td></tr></tbody></table>

### toText()

Convert html to plain text.

```php
$tp->toText(string);
```

### createConstants()

Convert `e_xxxxx` paths to their equivalent shortcodes. eg. `e_PLUGIN` becomes `{e_PLUGIN}`

```php
$tp->createConstants(string);
```

### replaceConstants()

Convert `{e_XXXX}` shortcode paths to their equivalent constants. eg. `{e_PLUGIN}` becomes `e_PLUGIN`

```php
$tp->replaceConstants(string);
```

### parseTemplate()

Parse an e107 template using core and/or custom shortcodes. ie. replaces all instances of `{XXXXX_XXXX}` etc.

```php
$tp->parseTemplate($template, true, $custom_shortcodes);
```

| Parameter            | Type    | Description |
| -------------------- | ------- | ----------- |
| template             | string  | ...         |
| user core shortcodes | boolean | ...         |
| custom shortcodes    | object  | ...         |

### thumbUrl()

Use to convert `{e_MEDIA_IMAGE}` and other image paths to an auto-sized image path for use inside an `<img>` tag.

```php
$url   = "{e_MEDIA_IMAGE}2012-04/someimage.jpg";
$image = $tp->thumbUrl($url);

echo "<img src='".$image."' />
```

### setThumbSize()

Set the width, height and crop of the thumbUrl function.

```php
$tp->setThumbSize($width, $height, $crop);
```

### toGlyph()

Convert a glyph name into Html. Just choose an icon from [Font Awesome](https://fontawesome.com/icons?d=gallery) and remove the first 'fa'\
Templates may also use the following shortcode: which calls the same function.

```php
$tp->toGlyph("fa-anchor");
```

#### Advanced settings:

```php
$tp->toGlyph("fa-anchor", array('size'=>'2x'));
```

### toIcon()

Render an icon. If a .glyph extension is found, it will automatically use the toGlyph() function above.

```php
$iconPath = "{e_MEDIA}myicon.png";
$tp->toIcon($iconPath);
```

### toAvatar()

Render a user avatar. If empty, the current user's avatar will be displayed if found or a generic avatar image.

```php
echo $tp->toAvatar(); // render avatar of the current user. 
```

```php
$userData = e107::user(5);  // Get User data for user-id #5. 
echo $tp->toAvatar($userData); // requires as a minimum $userData['user_image'].
```

### toImage()

Render an image.

```php
$url = "{e_MEDIA_IMAGE}2012-04/someimage.jpg";
$parms = array('w'=>500, 'h'=>200,'crop'=>1, 'alt'=>'my image'); // if not width/height set, the default as set by {SETIMAGE} will be used.
echo $tp->toImage($url,$parms); 
```

### lanVars()

Used for [substitution](https://devguide.e107.org/plugin-development/internationalisation#substitution) of variables, in [language files](https://devguide.e107.org/plugin-development/internationalisation) for example.&#x20;

```php
define("LAN_EXAMPLE_01", "Update results: [x] records changed, [y] errors, [z] not changed");

$repl = array($changed, $errors, $unchanged);
$text = $tp->lanVars(LAN_EXAMPLE_01, $repl);
```

## Parser options

{% hint style="info" %}
**TODO:** Convert below code into readable tables with proper descriptions
{% endhint %}

```php
// Set up the defaults
	private $e_optDefault = array(
		// default context: reflects legacy settings (many items enabled)
		'context'      => 'OLDDEFAULT',
		//
		'fromadmin'    => false,

		// Enable emote display
		'emotes'       => true,

		// Convert defines(constants) within text.
		'defs'         => false,

		// replace all {e_XXX} constants with their e107 value - 'rel' or 'abs'
		'constants'    => false,

		// Enable hooked parsers
		'hook'         => true,

		// Allow scripts through (new for 0.8)
		'scripts'      => true,

		// Make links clickable
		'link_click'   => true,

		// Substitute on clickable links (only if link_click == TRUE)
		'link_replace' => true,

		// Parse shortcodes - TRUE enables parsing
		'parse_sc'     => false,

		// remove HTML tags.
		'no_tags'      => false,

		// Restore entity form of quotes and such to single characters - TRUE disables
		'value'        => false,

		// Line break compression - TRUE removes newline characters
		'nobreak'      => false,

		// Retain newlines - wraps to \n instead of <br /> if TRUE (for non-HTML email text etc)
		'retain_nl'    => false
	);

```

## Parser modifiers

{% hint style="info" %}
**TODO:** Convert below code into readable tables with proper descriptions
{% endhint %}

```php
// Super modifiers override default option values
	private $e_SuperMods = array(
		//text is part of a title (e.g. news title)
		'TITLE'        =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'link_click' => false, 'emotes' => false, 'defs' => true, 'parse_sc' => true
			),
		'TITLE_PLAIN'  =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'link_click' => false, 'emotes' => false, 'defs' => true, 'parse_sc' => true, 'no_tags' => true
			),
		//text is user-entered (i.e. untrusted) and part of a title (e.g. forum title)
		'USER_TITLE'   =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'link_click' => false, 'scripts' => false, 'emotes' => false, 'hook' => false
			),
		// text is 'body' of email or similar - being sent 'off-site' so don't rely on server availability
		'E_TITLE'      =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'defs' => true, 'parse_sc' => true, 'emotes' => false, 'scripts' => false, 'link_click' => false
			),
		// text is part of the summary of a longer item (e.g. content summary)
		'SUMMARY'      =>
			array(
				'defs' => true, 'constants' => 'full', 'parse_sc' => true
			),
		// text is the description of an item (e.g. download, link)
		'DESCRIPTION'  =>
			array(
				'defs' => true, 'constants' => 'full', 'parse_sc' => true
			),
		// text is 'body' or 'bulk' text (e.g. custom page body, content body)
		'BODY'         =>
			array(
				'defs' => true, 'constants' => 'full', 'parse_sc' => true
			),
		// text is parsed by the Wysiwyg editor. eg. TinyMce
		'WYSIWYG'      =>
			array(
				'hook' => false, 'link_click' => false, 'link_replace' => false, 'retain_nl' => true
			),
		// text is user-entered (i.e. untrusted)'body' or 'bulk' text (e.g. custom page body, content body)
		'USER_BODY'    =>
			array(
				'constants' => 'full', 'scripts' => false, 'nostrip' => false
			),
		// text is 'body' of email or similar - being sent 'off-site' so don't rely on server availability
		'E_BODY'       =>
			array(
				'defs' => true, 'constants' => 'full', 'parse_sc' => true, 'emotes' => false, 'scripts' => false, 'link_click' => false
			),
		// text is text-only 'body' of email or similar - being sent 'off-site' so don't rely on server availability
		'E_BODY_PLAIN' =>
			array(
				'defs' => true, 'constants' => 'full', 'parse_sc' => true, 'emotes' => false, 'scripts' => false, 'link_click' => false, 'retain_nl' => true, 'no_tags' => true
			),
		// text is the 'content' of a link (A tag, etc)
		'LINKTEXT'     =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'link_click' => false, 'emotes' => false, 'hook' => false, 'defs' => true, 'parse_sc' => true
			),
		// text is used (for admin edit) without fancy conversions or html.
		'RAWTEXT'      =>
			array(
				'nobreak' => true, 'retain_nl' => true, 'link_click' => false, 'emotes' => false, 'hook' => false, 'no_tags' => true
			),
		'NODEFAULT'    =>
			array('context' => false, 'fromadmin' => false, 'emotes' => false, 'defs' => false, 'constants' => false, 'hook' => false,
			      'scripts' => false, 'link_click' => false, 'link_replace' => false, 'parse_sc' => false, 'no_tags' => false, 'value' => false,
			      'nobreak' => false, 'retain_nl' => false
			)
	);
```
