2025-01-12 04:36:52 +08:00

112 lines
4.8 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Customization of the <code>highr</code> package</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/utils/css/docco-classic.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/rstudio/markdown/inst/resources/prism-xcode.css">
</head>
<body>
<div class="frontmatter">
<div class="title"><h1>Customization of the <code>highr</code> package</h1></div>
<div class="author"><h2></h2></div>
<div class="date"><h3></h3></div>
</div>
<div class="body">
<!--
%\VignetteEngine{knitr::docco_classic}
%\VignetteIndexEntry{Customization of the highr package}
-->
<h1 id="customization-of-the-highr-package">Customization of the <code>highr</code> package</h1>
<p>If you are not satisfied with the default syntax highlighting commands in
the <strong>highr</strong> package, you can just use your own tags/commands. In this
vignette, we show a brief example.</p>
<p>The default highlighting commands are stored in two internal data frames
<code>cmd_latex</code> and <code>cmd_html</code>:</p>
<pre><code class="language-r">library(highr)
highr:::cmd_latex
</code></pre>
<pre><code>## cmd1 cmd2
## COMMENT \\hlcom{ }
## DEFAULT \\hldef{ }
## FUNCTION \\hlkwa{ }
## IF \\hlkwa{ }
## ELSE \\hlkwa{ }
## WHILE \\hlkwa{ }
## FOR \\hlkwa{ }
## IN \\hlkwa{ }
## BREAK \\hlkwa{ }
## REPEAT \\hlkwa{ }
## NEXT \\hlkwa{ }
## NULL_CONST \\hlkwa{ }
## LEFT_ASSIGN \\hlkwb{ }
## EQ_ASSIGN \\hlkwb{ }
## RIGHT_ASSIGN \\hlkwb{ }
## SYMBOL_FORMALS \\hlkwc{ }
## SYMBOL_SUB \\hlkwc{ }
## SLOT \\hlkwc{ }
## SYMBOL_FUNCTION_CALL \\hlkwd{ }
## NUM_CONST \\hlnum{ }
## '+' \\hlopt{ }
## '-' \\hlopt{ }
## '*' \\hlopt{ }
## '/' \\hlopt{ }
## '^' \\hlopt{ }
## '$' \\hlopt{ }
## '@' \\hlopt{ }
## ':' \\hlopt{ }
## '?' \\hlopt{ }
## '~' \\hlopt{ }
## '!' \\hlopt{ }
## SPECIAL \\hlopt{ }
## GT \\hlopt{ }
## GE \\hlopt{ }
## LT \\hlopt{ }
## LE \\hlopt{ }
## EQ \\hlopt{ }
## NE \\hlopt{ }
## AND \\hlopt{ }
## AND2 \\hlopt{ }
## OR \\hlopt{ }
## OR2 \\hlopt{ }
## NS_GET \\hlopt{ }
## NS_GET_INT \\hlopt{ }
## STR_CONST \\hlsng{ }
</code></pre>
<p>This data frame is passed to the <code>markup</code> argument in <code>hilight()</code>, so we are
free to pass a modified version there. Suppose I want to use the command
<code>\my&lt;*&gt;</code> instead of <code>\hl&lt;*&gt;</code>:</p>
<pre><code class="language-r">m = highr:::cmd_latex
m[, 1] = sub('\\hl', '\\my', m[, 1], fixed = TRUE)
head(m)
</code></pre>
<pre><code>## cmd1 cmd2
## COMMENT \\mycom{ }
## DEFAULT \\mydef{ }
## FUNCTION \\mykwa{ }
## IF \\mykwa{ }
## ELSE \\mykwa{ }
## WHILE \\mykwa{ }
</code></pre>
<p>Then</p>
<pre><code class="language-r">hilight(&quot;x = 1+1 # a comment&quot;) # default markup
</code></pre>
<pre><code>## [1] &quot;\\hldef{x} \\hlkwb{=} \\hlnum{1}\\hlopt{+}\\hlnum{1} \\hlcom{# a comment}&quot;
</code></pre>
<pre><code class="language-r">hilight(&quot;x = 1+1 # a comment&quot;, markup = m) # custom markup
</code></pre>
<pre><code>## [1] &quot;\\mydef{x} \\mykwb{=} \\mynum{1}\\myopt{+}\\mynum{1} \\mycom{# a comment}&quot;
</code></pre>
<p>This allows one to use arbitrary commands around the text symbols in the R
code. See <a href="https://github.com/yihui/highr/blob/master/R/highlight.R">https://github.com/yihui/highr/blob/master/R/highlight.R</a> for how
<code>cmd_latex</code> and <code>cmd_html</code> were generated in <strong>highr</strong>.</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/combine/npm/@xiee/utils/js/docco-classic.min.js,npm/@xiee/utils/js/docco-resize.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/@xiee/utils/js/center-img.min.js" defer></script>
</body>
</html>