Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for R Markdown (Rmd) files and to ignore R package metadata (i.e. non-code files) #294

Merged
merged 4 commits into from May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -1030,6 +1030,7 @@ RAML (raml)
RapydScript (pyj)
Razor (cshtml)
Rexx (rexx)
Rmd (Rmd)
RobotFramework (robot, tsv)
Ruby (rake, rb)
Ruby HTML (rhtml)
Expand Down
5 changes: 5 additions & 0 deletions Unix/t/00_C.t
Expand Up @@ -565,6 +565,11 @@ my @Tests = (
'ref' => '../tests/outputs/utilities.R.yaml',
'args' => '../tests/inputs/utilities.R',
},
{
'name' => 'Rmd',
'ref' => '../tests/output/test.Rmd.yaml',
'args' => '../tests/inputs/test.Rmd',
},
{
'name' => 'Ruby',
'ref' => '../tests/outputs/messages.rb.yaml',
Expand Down
32 changes: 32 additions & 0 deletions cloc
Expand Up @@ -5382,6 +5382,28 @@ sub remove_f90_comments { # {{{1
print "<- remove_f90_comments\n" if $opt_v > 2;
return @save_lines;
} # 1}}}
sub reduce_to_rmd_code_blocks { #{{{1
my ($ra_lines) = @_; #in
print "<- remove_f90_comments\n" if $opt_v > 2;
print "-> reduce_to_rmd_code_blocks()\n" if $opt_v > 2;

my $in_code_block = 0;
my @save_lines = ();
foreach (@{$ra_lines}) {
if ( m/^```\{\s*[[:alpha:]]/ ) {
$in_code_block = 1;
next;
}
if ( m/^```\s*$/ ) {
$in_code_block = 0;
}
next if (!$in_code_block);
push @save_lines, $_;
}

print "<- reduce_to_rmd_code_blocks()\n" if $opt_v> 2;
return @save_lines;
} # 1}}}
sub remove_matches { # {{{1
my ($ra_lines, # in
$pattern , # in Perl regular expression (case insensitive)
Expand Down Expand Up @@ -6434,6 +6456,7 @@ sub set_constants { # {{{1
'psm1' => 'PowerShell' ,
'R' => 'R' ,
'r' => 'R' ,
'Rmd' => 'Rmd' ,
'raml' => 'RAML' ,
'rkt' => 'Racket' ,
'rktl' => 'Racket' ,
Expand Down Expand Up @@ -7223,6 +7246,11 @@ sub set_constants { # {{{1
[ 'remove_matches' , '^\s*#' ],
[ 'remove_inline' , '#.*$' ],
],
'Rmd' => [
[ 'reduce_to_rmd_code_blocks' ],
[ 'remove_matches' , '^\s*#' ],
[ 'remove_inline' , '#.*$' ],
],
'Racket' => [
[ 'remove_matches' , '^\s*;' ],
[ 'remove_inline' , ';.*$' ],
Expand Down Expand Up @@ -7592,6 +7620,7 @@ sub set_constants { # {{{1
'PowerShell' => '\\\\$' ,
'Python' => '\\\\$' ,
'R' => '\\\\$' ,
'Rmd' => '\\\\$' ,
'Ruby' => '\\\\$' ,
'sed' => '\\\\$' ,
'Swift' => '\\\\$' ,
Expand Down Expand Up @@ -7686,13 +7715,15 @@ sub set_constants { # {{{1
'CHANGES' => 1,
'COPYING' => 1,
'COPYING' => 1,
'DESCRIPTION' => 1, # R packages metafile
'.cvsignore' => 1,
'Entries' => 1,
'FAQ' => 1,
'iconfig.h' => 1, # Skip "iconfig.h" files; they're used in Imakefiles.
'INSTALL' => 1,
'MAINTAINERS' => 1,
'MD5SUMS' => 1,
'NAMESPACE' => 1, # R packages metafile
'NEWS' => 1,
'readme' => 1,
'Readme' => 1,
Expand Down Expand Up @@ -8206,6 +8237,7 @@ sub set_constants { # {{{1
'quickbuild' => 2.86,
'quiz' => 5.33,
'R' => 3.00,
'Rmd' => 3.00,
'Racket' => 1.50,
'rally' => 2.00,
'ramis ii' => 2.00,
Expand Down
33 changes: 33 additions & 0 deletions tests/inputs/test.Rmd
@@ -0,0 +1,33 @@
---
title: "Untitled"
author: "cloc test"
date: "5/12/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
# summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)

plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
24 changes: 24 additions & 0 deletions tests/outputs/test.Rmd.yaml
@@ -0,0 +1,24 @@
1 text file.
1 unique file.
0 files ignored.

---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.77
elapsed_seconds : 0.00812411308288574
n_files : 1
n_lines : 33
files_per_second : 123.090359501101
lines_per_second : 4061.98186353632
Rmd :
nFiles: 1
blank: 10
comment: 19
code: 4
SUM:
blank: 10
comment: 19
code: 4
nFiles: 1