Skip to content

Commit

Permalink
added linkStyle option to render links as href
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed May 21, 2019
1 parent 42eb55c commit c2b13f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Markdown.php
Expand Up @@ -52,6 +52,17 @@ class Markdown extends \cebe\markdown\Parser
*/
public $labelPrefix = '';

const LINK_STYLE_FOOTNOTE = 'footnote';
const LINK_STYLE_HREF = 'href';

/**
* @var string link style defines how links are rendered in LaTeX, there are two different options:
*
* - `footnote` (default) - render all links with a footnote, which contains the full URL of the link. This is good for printing the PDF.
* - `href` - render all links with a hyperref, similar to HTML, the link target is not visible in this case.
*/
public $linkStyle = self::LINK_STYLE_FOOTNOTE;

/**
* @var array these are "escapeable" characters. When using one of these prefixed with a
* backslash, the character will be outputted without the backslash and is not interpreted
Expand Down Expand Up @@ -204,6 +215,9 @@ protected function renderLink($block)
}
return '\hyperref['.str_replace('#', '::', $url).']{' . $text . '}';
} else {
if ($this->linkStyle === self::LINK_STYLE_HREF) {
return '\href{' . $this->escapeUrl($url) . '}{' . $text . '}';
}
return $text . '\\footnote{' . (empty($block['title']) ? '' : $this->escapeLatex($block['title']) . ': ') . '\url{' . $this->escapeUrl($url) . '}}';
}
}
Expand Down
18 changes: 18 additions & 0 deletions bin/markdown-latex
Expand Up @@ -20,6 +20,7 @@ foreach ($composerAutoload as $autoload) {
// Send all errors to stderr
ini_set('display_errors', 'stderr');

$linkStyle = 'footnote';
$flavor = 'cebe\\markdown\\latex\\Markdown';
$flavors = [
'gfm' => ['cebe\\markdown\\latex\\GithubMarkdown', __DIR__ . '/../GithubMarkdown.php'],
Expand Down Expand Up @@ -47,6 +48,17 @@ foreach($argv as $k => $arg) {
error("Incomplete argument --flavor!", "usage");
}
break;
case '--link-style':
if (isset($arg[1])) {
if ($arg[1] === 'href' || $arg[1] === 'footnote') {
$linkStyle = $arg[1];
} else {
error("Unknown link style: " . $arg[1], "usage");
}
} else {
error("Incomplete argument --link-style!", "usage");
}
break;
case '--full':
$full = true;
break;
Expand Down Expand Up @@ -79,6 +91,7 @@ if (empty($src)) {

/** @var cebe\markdown\Parser $md */
$md = new $flavor();
$md->linkStyle = $linkStyle;
$tex = $md->parse($markdown);

if ($full) {
Expand Down Expand Up @@ -172,6 +185,11 @@ Usage:
gfm - Github flavored markdown [2]
extra - Markdown Extra [3]
--link-style specifies how links are being rendered:
footnote (default) - render all links with a footnote, which contains the full URL of the link. This is good for printing the PDF.
href - render all links with a hyperref, similar to HTML, the link target is not visible in this case.
--full ouput a full TEX document with document class, begin and end document. If not given, only the parsed markdown will be output.
--help shows this usage information.
Expand Down

0 comments on commit c2b13f5

Please sign in to comment.