tidy_html: Tidy or "Pretty Print" HTML/XHTML Documents

Description Usage Arguments Details Value Note References Examples

View source: R/tidy.r

Description

Pass in HTML content as either plain or raw text or parsed objects (either with the XML or xml2 packages) or as an httr response object along with an options list that specifies how the content will be tidied and get back tidied content of the same object type as passed in to the function.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## S3 method for class 'response'
tidy_html(content, options = list(TidyXhtmlOut =
  TRUE), verbose = FALSE)

tidy_html(content, options = list(TidyXhtmlOut = TRUE),
  verbose = FALSE)

## Default S3 method:
tidy_html(content, options = list(TidyXhtmlOut = TRUE),
  verbose = FALSE)

## S3 method for class 'character'
tidy_html(content, options = list(TidyXhtmlOut =
  TRUE), verbose = FALSE)

## S3 method for class 'raw'
tidy_html(content, options = list(TidyXhtmlOut = TRUE),
  verbose = FALSE)

## S3 method for class 'xml_document'
tidy_html(content, options = list(TidyXhtmlOut =
  TRUE), verbose = FALSE)

## S3 method for class 'HTMLInternalDocument'
tidy_html(content,
  options = list(TidyXhtmlOut = TRUE), verbose = FALSE)

## S3 method for class 'connection'
tidy_html(content, options = list(TidyXhtmlOut =
  TRUE), verbose = FALSE)

Arguments

content

accepts a character vector, raw vector or parsed content from the xml2 or XML packages.

options

named list of options

verbose

output document errors? (default: FALSE)

Details

The default option TixyXhtmlOut will convert the input content to XHTML.

Currently supported options:

File an issue if there are other libtidy options you'd like supported.

It is likely that the most used options will be:

You can clean up Microsoft Word (2000) and Google Docs HTML via logical settings for TidyWord2000 and TidyGDocClean, respectively.

It may also be advantageous to remove all comments with TidyHideComments.

Value

Tidied HTML/XHTML content. The object type will be the same as that of the input type except when it is a connection, then a character vector will be returned.

Note

If document parsing errors are severe enough, tidy_html() will not be able to clean the document and will display the errors (this output can be captured with sink() or capture.output()) along with a warning and return a "best effort" cleaned version of the document.

References

http://api.html-tidy.org/tidy/quickref_5.1.25.html & https://github.com/htacg/tidy-html5/blob/master/include/tidyenum.h for definitions of the options supported above and https://www.w3.org/People/Raggett/tidy/ for an explanation of what "tidy" HTML is and some canonical examples of what it can do.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
opts <- list(
  TidyDocType="html5",
  TidyMakeClean=TRUE,
  TidyHideComments=TRUE,
  TidyIndentContent=TRUE,
  TidyWrapLen=200
)

txt <- paste0(
  c("<html><head><style>p { color: red; }</style><body><!-- ===== body ====== -->",
"<p>Test</p></body><!--Default Zone --> <!--Default Zone End--></html>"),
  collapse="")

cat(tidy_html(txt, option=opts))

## Not run: 
library(httr)
res <- GET("https://rud.is/test/untidy.html")

# look at the original, un-tidy source
cat(content(res, as="text", encoding="UTF-8"))

# see the tidied version
cat(tidy_html(content(res, as="text", encoding="UTF-8"),
              list(TidyDocType="html5", TidyWrapLen=200)))

# but, you could also just do:
cat(tidy_html(url("https://rud.is/test/untidy.html")))

## End(Not run)

htmltidy documentation built on Oct. 4, 2019, 1:04 a.m.

Related to tidy_html in htmltidy...