Skip to content

Commit

Permalink
Add more syntax renderers etc.
Browse files Browse the repository at this point in the history
* Add more syntax renderers and tests
* Drop testing on MacOS and Windows
  • Loading branch information
samwilson authored Jul 14, 2022
1 parent 5224945 commit 9ea72d0
Show file tree
Hide file tree
Showing 34 changed files with 464 additions and 58 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jobs:

strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
php: [ '7.4', '8.0', '8.1' ]

runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest

name: PHP ${{ matrix.php }} on ${{ matrix.os }}
name: PHP ${{ matrix.php }}

steps:
- name: Checkout
Expand All @@ -30,7 +29,9 @@ jobs:
php-version: ${{matrix.php}}

- name: Install
run: composer install
run: |
sudo apt-get install texlive-latex-extra
composer install
- name: Test
run: composer test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
composer.lock
.phpcs-cache
.phpunit.result.cache
build/
vendor/
tests/data/CompileTest*

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see https://www.gnu.org/licenses/

## Kudos

* Test image by User:Scarce is Public Domain: https://commons.wikimedia.org/wiki/File:Simple_shapes_example.png
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/var-dumper": "^5.4",
"unleashedtech/php-coding-standard": "^3.1"
"unleashedtech/php-coding-standard": "^3.1",
"symfony/process": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/BlockQuoteRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
BlockQuote::assertInstanceOf($node);

return '\\begin{quotation}'
return '\\begin{quote}' . "\n"
. $childRenderer->renderNodes($node->children())
. '\\end{quotation}';
. '\\end{quote}';
}
}
23 changes: 23 additions & 0 deletions src/CodeRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Inline\Code;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class CodeRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
Code::assertInstanceOf($node);

return '\\texttt{' . $node->getLiteral() . '}';
}
}
31 changes: 31 additions & 0 deletions src/FencedCodeRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class FencedCodeRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
FencedCode::assertInstanceOf($node);

$lang = '';
$infoWords = $node->getInfoWords();
if (\count($infoWords) > 0) {
$lang = $infoWords[0];
}

return '\\lstset{language={' . $lang . '}}\\begin{lstlisting}' . "\n"
. $node->getLiteral()
. '\\end{lstlisting}';
}
}
6 changes: 4 additions & 2 deletions src/HeadingRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class HeadingRenderer implements NodeRendererInterface
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
$sec = 'section';
if ($node->getDepth() === 2) {
if ($node->getLevel() === 2) {
$sec = 'subsection';
} elseif ($node->getDepth() === 3) {
} elseif ($node->getLevel() === 3) {
$sec = 'subsubsection';
} elseif ($node->getLevel() === 4) {
$sec = 'paragraph';
}

return '\\' . $sec . '{' . $childRenderer->renderNodes($node->children()) . '}';
Expand Down
23 changes: 23 additions & 0 deletions src/ImageRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class ImageRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
Image::assertInstanceOf($node);

return '\includegraphics{' . $node->getUrl() . '}';
}
}
25 changes: 25 additions & 0 deletions src/IndentedCodeRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class IndentedCodeRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
IndentedCode::assertInstanceOf($node);

return '\\lstset{language={}}\\begin{lstlisting}' . "\n"
. $node->getLiteral()
. '\\end{lstlisting}';
}
}
36 changes: 21 additions & 15 deletions src/LatexRendererExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@

use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Extension\CommonMark\Node\Block\ThematicBreak;
use League\CommonMark\Extension\CommonMark\Node\Inline\Code;
use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis;
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
use League\CommonMark\Extension\ExtensionInterface;
use League\CommonMark\Node\Block\Paragraph;
Expand All @@ -19,24 +27,22 @@ public function register(EnvironmentBuilderInterface $environment): void
{
$environment
->addInlineParser(new LatexSpecialCharsParser(), 10)

->addRenderer(Paragraph::class, new ParagraphRenderer(), 10)
->addRenderer(Text::class, new TextRenderer(), 10)

->addRenderer(BlockQuote::class, new BlockQuoteRenderer(), 10)
// @todo ->addRenderer(CoreNode\Block\Document::class, new CoreRenderer\Block\DocumentRenderer(), 10)
// @todo ->addRenderer(Node\Block\FencedCode::class, new Renderer\Block\FencedCodeRenderer(), 10)
->addRenderer(FencedCode::class, new FencedCodeRenderer(), 10)
->addRenderer(Heading::class, new HeadingRenderer(), 10)
// @todo ->addRenderer(Node\Block\HtmlBlock::class, new Renderer\Block\HtmlBlockRenderer(), 10)
// @todo ->addRenderer(Node\Block\IndentedCode::class, new Renderer\Block\IndentedCodeRenderer(), 10)
// @todo ->addRenderer(Node\Block\ListBlock::class, new Renderer\Block\ListBlockRenderer(), 10)
// @todo ->addRenderer(Node\Block\ListItem::class, new Renderer\Block\ListItemRenderer(), 10)
->addRenderer(Paragraph::class, new ParagraphRenderer(), 10)
// @todo ->addRenderer(Node\Block\ThematicBreak::class, new Renderer\Block\ThematicBreakRenderer(), 10)
->addRenderer(IndentedCode::class, new IndentedCodeRenderer(), 10)
->addRenderer(ListBlock::class, new ListBlockRenderer(), 10)
->addRenderer(ListItem::class, new ListItemRenderer(), 10)
->addRenderer(ThematicBreak::class, new ThematicBreakRenderer(), 10)

// @todo ->addRenderer(Node\Inline\Code::class, new Renderer\Inline\CodeRenderer(), 10)
->addRenderer(Code::class, new CodeRenderer(), 10)
->addRenderer(Emphasis::class, new EmphasisRenderer(), 10)
// @todo ->addRenderer(Node\Inline\HtmlInline::class, new Renderer\Inline\HtmlInlineRenderer(), 10)
// @todo ->addRenderer(Node\Inline\Image::class, new Renderer\Inline\ImageRenderer(), 10)
// @todo ->addRenderer(Node\Inline\Link::class, new Renderer\Inline\LinkRenderer(), 10)
// @todo ->addRenderer(CoreNode\Inline\Newline::class, new CoreRenderer\Inline\NewlineRenderer(), 10)
->addRenderer(Strong::class, new StrongRenderer(), 10)
->addRenderer(Text::class, new TextRenderer(), 10);
->addRenderer(Image::class, new ImageRenderer(), 10)
->addRenderer(Link::class, new LinkRenderer(), 10)
->addRenderer(Strong::class, new StrongRenderer(), 10);
}
}
52 changes: 52 additions & 0 deletions src/LinkRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\RegexHelper;
use League\Config\ConfigurationAwareInterface;
use League\Config\ConfigurationInterface;

class LinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface
{
private ConfigurationInterface $config;

public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}

/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
Link::assertInstanceOf($node);

$out = $childRenderer->renderNodes($node->children());

$url = \str_replace('_', '\\_', $node->getUrl());

$allowUnsafeLinks = $this->config->get('allow_unsafe_links');
if (! $allowUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($url)) {
return $out;
}

// Autolink extension makes the label and the URL the same.
if ($out === $url) {
return '\\url{' . $url . '}';
}

$title = $node->getTitle();
if ($title) {
$title .= ': ';
}

return $out . '\\footnote{' . $title . '\\url{' . $url . '}}';
}
}
28 changes: 28 additions & 0 deletions src/ListBlockRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class ListBlockRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
ListBlock::assertInstanceOf($node);

$listType = $node->getListData()->type === ListBlock::TYPE_BULLET ? 'itemize' : 'enumerate';
$innerSeparator = $childRenderer->getInnerSeparator();

return '\\begin{' . $listType . '}' . "\n"
. $innerSeparator . $childRenderer->renderNodes($node->children()) . $innerSeparator
. '\\end{' . $listType . '}';
}
}
23 changes: 23 additions & 0 deletions src/ListItemRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class ListItemRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
ListItem::assertInstanceOf($node);

return '\\item ' . $childRenderer->renderNodes($node->children());
}
}
23 changes: 23 additions & 0 deletions src/ThematicBreakRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Samwilson\CommonMarkLatex;

use League\CommonMark\Extension\CommonMark\Node\Block\ThematicBreak;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class ThematicBreakRenderer implements NodeRendererInterface
{
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
ThematicBreak::assertInstanceOf($node);

return "\n" . '\\noindent\\rule{\\textwidth}{0.4pt}';
}
}
Binary file added tests/data/Simple_shapes_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/data/blockquote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
> A two-line
> quote.
> Another, one line this time.
Para in between.

> Multi-paragraph
>
> quote.
14 changes: 14 additions & 0 deletions tests/data/blockquote.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
\begin{quote}
A two-line
quote.
\end{quote}
\begin{quote}
Another, one line this time.
\end{quote}
Para in between.

\begin{quote}
Multi-paragraph

quote.
\end{quote}
Loading

0 comments on commit 9ea72d0

Please sign in to comment.