Skip to content

Commit

Permalink
Fixes for HTML 5 (#1319)
Browse files Browse the repository at this point in the history
* Use style to float package logo
* Ensure \preformatted start paragraphs
* Generate pure HTML in R7 inherited methods summary
* Disambiguate methods with classname
  • Loading branch information
hadley authored Apr 8, 2022
1 parent 4473f2a commit ea7d27f
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 110 deletions.
1 change: 1 addition & 0 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ mdxml_code_block <- function(xml, state) {
info <- xml_attr(xml, "info")[1]
if (is.na(info) || nchar(info[1]) == 0) info <- NA_character_
paste0(
"\n\n",
if (!is.na(info)) paste0("\\if{html}{\\out{<div class=\"sourceCode ", info, "\">}}"),
"\\preformatted{",
escape_verb(xml_text(xml)),
Expand Down
2 changes: 1 addition & 1 deletion R/object-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object_defaults.package <- function(x, block) {
description <- package_url_parse(description)
logo_path <- file.path(x$value$path, "man", "figures", "logo.png")
if (file.exists(logo_path)) {
fig <- "\\if{html}{\\figure{logo.png}{options: align='right' alt='logo' width='120'}}"
fig <- "\\if{html}{\\figure{logo.png}{options: style='float: right' alt='logo' width='120'}}"
description <- paste0(fig, "\n\n", description)
}

Expand Down
4 changes: 4 additions & 0 deletions R/object-r6.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extract_r6_methods <- function(x) {
methods <- data.frame(
stringsAsFactors = FALSE,
type = if (length(method_loc)) "method" else character(),
class = if (length(method_loc)) x$classname %||% NA_character_ else character(),
name = unname(method_nms),
file = unname(method_fnm),
line = unname(method_loc),
Expand All @@ -91,6 +92,7 @@ add_default_method_data <- function(obj, methods) {
rec <- data.frame(
stringsAsFactors = FALSE,
type = defaults[[mname]]$type %||% "method",
class = defaults[[mname]]$class %||% obj$classname %||% "unknown",
name = defaults[[mname]]$name %||% mname,
file = defaults[[mname]]$file %||% NA_character_,
line = defaults[[mname]]$line %||% NA_integer_,
Expand All @@ -108,6 +110,7 @@ extract_r6_fields <- function(x) {
stringsAsFactors = FALSE,
type = rep("field", length(field_nms)),
name = as.character(field_nms),
class = rep(x$classname %||% NA_character_, length(field_nms)),
file = rep(NA, length(field_nms)),
line = rep(NA, length(field_nms)),
formals = I(replicate(length(field_nms), NULL))
Expand All @@ -120,6 +123,7 @@ extract_r6_bindings <- function(x) {
stringsAsFactors = FALSE,
type = if (length(bind_nms)) "active" else character(),
name = as.character(bind_nms),
class = rep(x$classname %||% NA_character_, length(bind_nms)),
file = rep(NA, length(bind_nms)),
line = rep(NA, length(bind_nms)),
formals = I(replicate(length(bind_nms), NULL))
Expand Down
31 changes: 16 additions & 15 deletions R/rd-r6.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ r6_method_list <- function(block, methods) {
c("\\subsection{Public methods}{",
"\\itemize{",
sprintf(
"\\item \\href{#method-%s}{\\code{%s$%s()}}",
"\\item \\href{#method-%s-%s}{\\code{%s$%s()}}",
methods$class,
nms,
block$object$alias,
nms
Expand All @@ -261,43 +262,43 @@ r6_inherited_method_list <- function(block, r6data) {
super_meth <- super_meth[rev(seq_len(nrow(super_meth))), ]

details <- paste0(
"<details ",
if (nrow(super_meth) <= 5) "open ",
"<details",
if (nrow(super_meth) <= 5) " open",
"><summary>Inherited methods</summary>"
)

c("\\if{html}{",
paste0("\\out{", details, "}"),
"\\itemize{",
c("\\if{html}{\\out{", details,
"<ul>",
sprintf(
paste0(
"\\item \\out{<span class=\"pkg-link\" data-pkg=\"%s\" ",
"data-topic=\"%s\" data-id=\"%s\">}",
"\\href{../../%s/html/%s.html#method-%s}{\\code{%s::%s$%s()}}",
"\\out{</span>}"
"<li>",
"<span class=\"pkg-link\" data-pkg=\"%s\" data-topic=\"%s\" data-id=\"%s\">",
"<a href='../../%s/html/%s.html#method-%s-%s'><code>%s::%s$%s()</code></a>",
"</li>"
),
super_meth$package,
super_meth$classname,
super_meth$name,
super_meth$package,
super_meth$classname,
super_meth$classname,
super_meth$name,
super_meth$package,
super_meth$classname,
super_meth$name
),
"}",
"\\out{</details>}",
"}"
"</ul>",
"</details>",
"}}"
)
}

r6_method_begin <- function(block, method) {
nm <- r6_show_name(method$name)
c(
"\\if{html}{\\out{<hr>}}",
paste0("\\if{html}{\\out{<a id=\"method-", nm, "\"></a>}}"),
paste0("\\if{latex}{\\out{\\hypertarget{method-", nm, "}{}}}"),
paste0("\\if{html}{\\out{<a id=\"method-", method$class, "-", nm, "\"></a>}}"),
paste0("\\if{latex}{\\out{\\hypertarget{method-", method$class, "-", nm, "}{}}}"),
paste0("\\subsection{Method \\code{", nm, "()}}{")
)
}
Expand Down
4 changes: 3 additions & 1 deletion man/load.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/load_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions man/markdown_pass1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/roxygen2-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/markdown.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# code blocks work

Before

\preformatted{x \%in\% 1:10
}

After

# code block with language creates HTML tag

Before

\if{html}{\out{<div class="sourceCode r">}}\preformatted{x \%in\% 1:10
}\if{html}{\out{</div>}}

After

# can convert table to Rd

Code
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/roxygen-block-3-A.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ Class A details
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-meth1}{\code{A$meth1()}}
\item \href{#method-meth2}{\code{A$meth2()}}
\item \href{#method-meth3}{\code{A$meth3()}}
\item \href{#method-clone}{\code{A$clone()}}
\item \href{#method-A-meth1}{\code{A$meth1()}}
\item \href{#method-A-meth2}{\code{A$meth2()}}
\item \href{#method-A-meth3}{\code{A$meth3()}}
\item \href{#method-A-clone}{\code{A$clone()}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth1"></a>}}
\if{latex}{\out{\hypertarget{method-meth1}{}}}
\if{html}{\out{<a id="method-A-meth1"></a>}}
\if{latex}{\out{\hypertarget{method-A-meth1}{}}}
\subsection{Method \code{meth1()}}{
A method 1.
\subsection{Usage}{
Expand All @@ -81,8 +81,8 @@ A method 1.

}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth2"></a>}}
\if{latex}{\out{\hypertarget{method-meth2}{}}}
\if{html}{\out{<a id="method-A-meth2"></a>}}
\if{latex}{\out{\hypertarget{method-A-meth2}{}}}
\subsection{Method \code{meth2()}}{
Method 2 description.
\subsection{Usage}{
Expand Down Expand Up @@ -112,8 +112,8 @@ Method 2 details.

}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth3"></a>}}
\if{latex}{\out{\hypertarget{method-meth3}{}}}
\if{html}{\out{<a id="method-A-meth3"></a>}}
\if{latex}{\out{\hypertarget{method-A-meth3}{}}}
\subsection{Method \code{meth3()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{A$meth3(duplicate, missing)}\if{html}{\out{</div>}}
Expand All @@ -137,8 +137,8 @@ twice.
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\if{html}{\out{<a id="method-A-clone"></a>}}
\if{latex}{\out{\hypertarget{method-A-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
Expand Down
38 changes: 19 additions & 19 deletions tests/testthat/roxygen-block-3-B.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ Class B details.
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-meth1}{\code{B$meth1()}}
\item \href{#method-meth4}{\code{B$meth4()}}
\item \href{#method-clone}{\code{B$clone()}}
}
}
\if{html}{
\out{<details open ><summary>Inherited methods</summary>}
\itemize{
\item \out{<span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth2">}\href{../../roxygen2/html/A.html#method-meth2}{\code{roxygen2::A$meth2()}}\out{</span>}
\item \out{<span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth3">}\href{../../roxygen2/html/A.html#method-meth3}{\code{roxygen2::A$meth3()}}\out{</span>}
}
\out{</details>}
}
\item \href{#method-B-meth1}{\code{B$meth1()}}
\item \href{#method-B-meth4}{\code{B$meth4()}}
\item \href{#method-B-clone}{\code{B$clone()}}
}
}
\if{html}{\out{
<details open><summary>Inherited methods</summary>
<ul>
<li><span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth2"><a href='../../roxygen2/html/A.html#method-A-meth2'><code>roxygen2::A$meth2()</code></a></li>
<li><span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth3"><a href='../../roxygen2/html/A.html#method-A-meth3'><code>roxygen2::A$meth3()</code></a></li>
</ul>
</details>
}}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth1"></a>}}
\if{latex}{\out{\hypertarget{method-meth1}{}}}
\if{html}{\out{<a id="method-B-meth1"></a>}}
\if{latex}{\out{\hypertarget{method-B-meth1}{}}}
\subsection{Method \code{meth1()}}{
B method 1.
\subsection{Usage}{
Expand All @@ -67,8 +67,8 @@ B method 1.
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth4"></a>}}
\if{latex}{\out{\hypertarget{method-meth4}{}}}
\if{html}{\out{<a id="method-B-meth4"></a>}}
\if{latex}{\out{\hypertarget{method-B-meth4}{}}}
\subsection{Method \code{meth4()}}{
A method 4.
\subsection{Usage}{
Expand All @@ -77,8 +77,8 @@ A method 4.

}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\if{html}{\out{<a id="method-B-clone"></a>}}
\if{latex}{\out{\hypertarget{method-B-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
Expand Down
40 changes: 20 additions & 20 deletions tests/testthat/roxygen-block-3-C.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ Classs C details.
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-meth2}{\code{C$meth2()}}
\item \href{#method-meth5}{\code{C$meth5()}}
\item \href{#method-undocumented_method}{\code{C$undocumented_method()}}
}
}
\if{html}{
\out{<details open ><summary>Inherited methods</summary>}
\itemize{
\item \out{<span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth3">}\href{../../roxygen2/html/A.html#method-meth3}{\code{roxygen2::A$meth3()}}\out{</span>}
\item \out{<span class="pkg-link" data-pkg="roxygen2" data-topic="B" data-id="meth1">}\href{../../roxygen2/html/B.html#method-meth1}{\code{roxygen2::B$meth1()}}\out{</span>}
\item \out{<span class="pkg-link" data-pkg="roxygen2" data-topic="B" data-id="meth4">}\href{../../roxygen2/html/B.html#method-meth4}{\code{roxygen2::B$meth4()}}\out{</span>}
}
\out{</details>}
}
\item \href{#method-C-meth2}{\code{C$meth2()}}
\item \href{#method-C-meth5}{\code{C$meth5()}}
\item \href{#method-C-undocumented_method}{\code{C$undocumented_method()}}
}
}
\if{html}{\out{
<details open><summary>Inherited methods</summary>
<ul>
<li><span class="pkg-link" data-pkg="roxygen2" data-topic="A" data-id="meth3"><a href='../../roxygen2/html/A.html#method-A-meth3'><code>roxygen2::A$meth3()</code></a></li>
<li><span class="pkg-link" data-pkg="roxygen2" data-topic="B" data-id="meth1"><a href='../../roxygen2/html/B.html#method-B-meth1'><code>roxygen2::B$meth1()</code></a></li>
<li><span class="pkg-link" data-pkg="roxygen2" data-topic="B" data-id="meth4"><a href='../../roxygen2/html/B.html#method-B-meth4'><code>roxygen2::B$meth4()</code></a></li>
</ul>
</details>
}}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth2"></a>}}
\if{latex}{\out{\hypertarget{method-meth2}{}}}
\if{html}{\out{<a id="method-C-meth2"></a>}}
\if{latex}{\out{\hypertarget{method-C-meth2}{}}}
\subsection{Method \code{meth2()}}{
C method 2.
\subsection{Usage}{
Expand All @@ -80,8 +80,8 @@ C method 2.
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-meth5"></a>}}
\if{latex}{\out{\hypertarget{method-meth5}{}}}
\if{html}{\out{<a id="method-C-meth5"></a>}}
\if{latex}{\out{\hypertarget{method-C-meth5}{}}}
\subsection{Method \code{meth5()}}{
C method 5.
\subsection{Usage}{
Expand All @@ -90,8 +90,8 @@ C method 5.

}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-undocumented_method"></a>}}
\if{latex}{\out{\hypertarget{method-undocumented_method}{}}}
\if{html}{\out{<a id="method-C-undocumented_method"></a>}}
\if{latex}{\out{\hypertarget{method-C-undocumented_method}{}}}
\subsection{Method \code{undocumented_method()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{C$undocumented_method()}\if{html}{\out{</div>}}
Expand Down
Loading

0 comments on commit ea7d27f

Please sign in to comment.