Skip to content

Commit

Permalink
Fixes #20. Add support for more characters by adding parser options
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver authored Dec 19, 2016
1 parent 6ae3d2a commit 8680047
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
37 changes: 33 additions & 4 deletions Helper/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,31 @@ public function __construct() {
/**
* Parse the text and replace known special latex characters correctly
*
* @param string $text
* @param boolean $checkTable
* @param string $text The string that needs to be parsed
* @param boolean $checkTable If set, tables should be detected automatically (default true)
* @param boolean $removeLatex If set, all LaTeX commands will be removed (default false)
* @param boolean $parseNewLines If set, newline characters will be replaced by LaTeX entities (default false)
*
* @return mixed
*/
public function parseText($text, $checkTable = true) {
public function parseText($text, $checkTable = true, $removeLatex = false, $parseNewLines = false) {

// Try to replace HTML entities
preg_match_all('/&[a-zA-Z]+;/iu', $text, $matches);
foreach ($matches[0] as $match) {
$text = str_replace($match, $this->htmlCodes[$match], $text);
}

// LaTeX command characters if required
if ($removeLatex) {
$text = str_replace("\\", "\\backslash ", $text);
$text = str_replace("{", "\\{ ", $text);
$text = str_replace("}", "\\} ", $text);
}

// Special characters
$text = str_replace('²', '\\textsuperscript{2}', $text);
$text = str_replace('³', '\\textsuperscript{3}', $text);

$text = str_replace('²', '\\textsuperscript{2}', $text);
$text = str_replace('³', '\\textsuperscript{3}', $text);

Expand Down Expand Up @@ -102,8 +112,27 @@ public function parseText($text, $checkTable = true) {
$text = str_replace("Ø", "{\\O}", $text);
$text = str_replace("ø", "{\\o}", $text);

// Remaining special characters (cannot be placed with the others,
// as then the html entity replace would fail).
$text = str_replace("#", "\\#", $text);
$text = str_replace("_", "\\_", $text);
$text = str_replace("^", "\\^{}", $text);
$text = str_replace("°", "\$^{\\circ}\$", $text);
$text = str_replace(">", "\\textgreater ", $text);
$text = str_replace("<", "\\textless ", $text);
$text = str_replace("~", "\\textasciitilde ", $text);

// New lines if required
if ($parseNewLines) {
$text = str_replace("\\n", "\\newline ", $text);
$text = str_replace(PHP_EOL, "\\newline ", $text);
}

// Remove last Latex characters if required
if ($removeLatex) {
$text = str_replace("%", "\\%", $text);
$text = str_replace("$", "\\$", $text);
}

// Check for & characters. Inside a tabular(x) env they should not be replaced
$offset = 0;
Expand Down
12 changes: 8 additions & 4 deletions Resources/doc/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,17 @@ The last exception also includes a backtrace in it message which can be used to
## Character escaping
---------------------------

This bundle includes a simple text parser which can escape most UTF-8 characters like ö to \"o. The method `parseText` in the `Helper/Parser` class takes the text to parse as argument and return the parsed text.
This bundle includes a simple text parser which can escape most UTF-8 characters like ö to \"o. The method `parseText` in the `Helper/Parser` class takes the text to parse as argument and return the parsed text.

This method is also available in Twig as a simple filter: `latex_escape`. This filter is applied automatically when using the standard objects/templates.
> **Note**: By default, the filter is not meant to remove latex commands! It is meant to convert characters that might be used by your users into a character that LaTeX understands. However, when the parameters are set correctly, you can achieve the remove all behavior.
The filter takes a single argument, which is by default true. When set to false, the check for the & char is disabled, which might by handy in table environments.
This method is also available in Twig as a simple filter: `latex_escape`. This filter is applied automatically when using the standard objects/templates. Note that the filter takes arguments which can change the behavior of the text parser. For the most up-to-date arguments check the [`Parser` class](https://github.com/bobvandevijver/latex-bundle/blob/master/Helper/Parser.php#L34).

If you have any character that generates an error, feel create an issue or create a PR.
Next to the `latex_escape` filter, there is also a `latex_escape_all` filter. This should remove all LaTeX commands.

The parser takes three arguments: `checkTable`, `removeLatex` and `parseNewLines`. The first is by default `true`, while the other two are `false` by default. If you do not want to use the table checking (the check for the & char is disabled, which might by handy in table environments), set the first argument to `false`. To remove all Latex commands from the input, set the second parameter to `true`. This disables the possibility to use custom commands from your input. The third argument can be set to `true` in order to replace all newline characters into LaTeX commands for newlines.

If you have any character that generates an error, feel create [an issue](https://github.com/bobvandevijver/latex-bundle/issues/new) or create a PR.

## HTML to LaTeX
---------------------------
Expand Down
22 changes: 16 additions & 6 deletions Twig/BobVLatexExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,37 @@ class BobVLatexExtension extends \Twig_Extension

private $parser;

public function __construct(){
public function __construct() {
$this->parser = new Parser();
}

/**
* @return array
*/
public function getFilters()
{
public function getFilters() {
return array(
new \Twig_SimpleFilter('latex_escape', array($this->parser, 'parseText')),
new \Twig_SimpleFilter('latex_parse_html', array($this->parser, 'parseHtml'))
new \Twig_SimpleFilter('latex_escape_all', array($this, 'latexEscapeAll')),
new \Twig_SimpleFilter('latex_parse_html', array($this->parser, 'parseHtml')),
);
}

/**
* @return string
*/
public function getName()
{
public function getName() {
return 'bobv_latex_twig_extension';
}

/**
* Proxy method to set some params for the parseText call
*
* @param $text
*
* @return mixed
*/
public function latexEscapeAll($text) {
return $this->parser->parseText($text, false, true, true);
}

}

0 comments on commit 8680047

Please sign in to comment.