Browse Source

support file and vector of CSS

master
boB Rudis 5 years ago
parent
commit
b4ee3fa92c
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 3
      NAMESPACE
  2. 26
      R/main.R
  3. 32
      README.Rmd
  4. 62
      README.md
  5. 14
      man/parse_css_file.Rd
  6. 14
      man/parse_css_text.Rd

3
NAMESPACE

@ -1,7 +1,8 @@
# Generated by roxygen2: do not edit by hand
export("%>%")
export(parse_css)
export(parse_css_file)
export(parse_css_text)
importFrom(Rcpp,sourceCpp)
importFrom(dplyr,mutate)
importFrom(magrittr,"%>%")

26
R/main.R

@ -1,7 +1,4 @@
#' @export
parse_css <- function(fil) {
x <- paste0(readLines(fil), collapse="\n")
.doparse <- function(x) {
ret <- .Call(`_tsuka_parse_css`, x)
@ -16,3 +13,24 @@ parse_css <- function(fil) {
})
}
#' Parse CSS text intoa data frame
#'
#' @param css a character vector of CSS fulrs
#' @export
parse_css_text <- function(css) {
.doparse(paste0(css, collapse="\n"))
}
#' Parse a CSS file into a data frame
#'
#' @param fil path to CSS file (will be [path.expand()]ed)
#' @export
parse_css_file <- function(fil) {
.doparse(paste0(readLines(fil), collapse="\n"))
}

32
README.Rmd

@ -17,7 +17,7 @@ hrbrpkghelpr::yank_title_and_description()
## SOME IMPORTANT DETAILS
Threw this together for [\@clauswilke](https://github.com/clauswilke) & [{ggtext}](https://github.com/clauswilke/ggtext) based on [this Twitter convo](https://twitter.com/hrbrmstr/status/1159920814801731586). I am unlikely to work on this more w/o serious prodding.
Threw this together for [\@clauswilke](https://github.com/clauswilke) & [{ggtext}](https://github.com/clauswilke/ggtext) based on [this Twitter convo](https://twitter.com/hrbrmstr/status/1159920814801731586). I am unlikely to work on this more w/o serious prodding. It only handles CSS style rules and need 7 other enums covered from [`katana`](https://github.com/hackers-painters/katana-parser/blob/master/src/katana.h#L38-L48), plus the core cpp code need to be refactored into functions.
You need to install the [`katana`](https://github.com/hackers-painters/katana-parser) C99 parser as a system library that is reachable by the R source package compilation defaults. It looks to be pretty straightforward to embed the `katana` source into a C[++]-backed R package but that wasn't the point of this exercise.
@ -47,7 +47,35 @@ packageVersion("tsuka")
```
```{r ex1}
parse_css(system.file("extdat/sample.css", package = "tsuka"))
"
/* Applies to the entire body of the HTML document (except where overridden by more specific
selectors). */
body {
margin: 25px;
background-color: rgb(240,240,240);
font-family: arial, sans-serif;
font-size: 14px;
}
/* Applies to all <h1>...</h1> elements. */
h1 {
font-size: 35px;
font-weight: normal;
margin-top: 5px;
}
/* Applies to all elements with <... class='someclass'> specified. */
.someclass { color: red; }
/* Applies to the element with <... id='someid'> specified. */
#someid { color: green; }
" -> css_ex
parse_css_text(css_ex)
```
```{r ex2}
parse_css_file(system.file("extdat/sample.css", package = "tsuka"))
```
## tsuka Metrics

62
README.md

@ -30,7 +30,10 @@ parse CSS content into data frames.
Threw this together for [@clauswilke](https://github.com/clauswilke) &
[{ggtext}](https://github.com/clauswilke/ggtext) based on [this Twitter
convo](https://twitter.com/hrbrmstr/status/1159920814801731586). I am
unlikely to work on this more w/o serious prodding.
unlikely to work on this more w/o serious prodding. It only handles CSS
style rules and need 7 other enums covered from
[`katana`](https://github.com/hackers-painters/katana-parser/blob/master/src/katana.h#L38-L48),
plus the core cpp code need to be refactored into functions.
You need to install the
[`katana`](https://github.com/hackers-painters/katana-parser) C99 parser
@ -46,6 +49,9 @@ enable use of the same utility functions they use.
The following functions are implemented:
- `parse_css_file`: Parse a CSS file into a data frame
- `parse_css_text`: Parse CSS text intoa data frame
## Installation
``` r
@ -73,7 +79,51 @@ packageVersion("tsuka")
```
``` r
parse_css(system.file("extdat/sample.css", package = "tsuka"))
"
/* Applies to the entire body of the HTML document (except where overridden by more specific
selectors). */
body {
margin: 25px;
background-color: rgb(240,240,240);
font-family: arial, sans-serif;
font-size: 14px;
}
/* Applies to all <h1>...</h1> elements. */
h1 {
font-size: 35px;
font-weight: normal;
margin-top: 5px;
}
/* Applies to all elements with <... class='someclass'> specified. */
.someclass { color: red; }
/* Applies to the element with <... id='someid'> specified. */
#someid { color: green; }
" -> css_ex
parse_css_text(css_ex)
```
<div class="kable-table">
| selectors | property | value |
| :--------- | :--------------- | :------------------- |
| body | margin | 25px |
| body | background-color | rgb(240 , 240 , 240) |
| body | font-family | arial , sans-serif |
| body | font-size | 14px |
| h1 | font-size | 35px |
| h1 | font-weight | normal |
| h1 | margin-top | 5px |
| .someclass | color | red |
| \#someid | color | green |
</div>
``` r
parse_css_file(system.file("extdat/sample.css", package = "tsuka"))
```
<div class="kable-table">
@ -317,10 +367,10 @@ parse_css(system.file("extdat/sample.css", package = "tsuka"))
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :----------- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| C/C++ Header | 6 | 0.43 | 580 | 0.74 | 238 | 0.80 | 245 | 0.77 |
| C++ | 2 | 0.14 | 168 | 0.22 | 32 | 0.11 | 7 | 0.02 |
| R | 5 | 0.36 | 22 | 0.03 | 9 | 0.03 | 33 | 0.10 |
| Rmd | 1 | 0.07 | 9 | 0.01 | 19 | 0.06 | 34 | 0.11 |
| C/C++ Header | 6 | 0.43 | 580 | 0.72 | 238 | 0.77 | 245 | 0.74 |
| C++ | 2 | 0.14 | 168 | 0.21 | 32 | 0.10 | 7 | 0.02 |
| Rmd | 1 | 0.07 | 29 | 0.04 | 24 | 0.08 | 37 | 0.11 |
| R | 5 | 0.36 | 27 | 0.03 | 15 | 0.05 | 40 | 0.12 |
## Code of Conduct

14
man/parse_css_file.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/main.R
\name{parse_css_file}
\alias{parse_css_file}
\title{Parse a CSS file into a data frame}
\usage{
parse_css_file(fil)
}
\arguments{
\item{fil}{path to CSS file (will be \code{\link[=path.expand]{path.expand()}}ed)}
}
\description{
Parse a CSS file into a data frame
}

14
man/parse_css_text.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/main.R
\name{parse_css_text}
\alias{parse_css_text}
\title{Parse CSS text intoa data frame}
\usage{
parse_css_text(css)
}
\arguments{
\item{css}{a character vector of CSS fulrs}
}
\description{
Parse CSS text intoa data frame
}
Loading…
Cancel
Save