From d38457eb382c6b6278789685d48fe0bd31e589c4 Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sat, 12 May 2018 18:31:11 -0400 Subject: [PATCH 1/4] Add support for R Markdown (Rmd) files and to ignore R package metadata (i.e. non-code) files --- README.md | 1 + Unix/t/00_C.t | 5 +++++ cloc | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 11a90972..489774f9 100644 --- a/README.md +++ b/README.md @@ -1030,6 +1030,7 @@ RAML (raml) RapydScript (pyj) Razor (cshtml) Rexx (rexx) +Rmd (Rmd) RobotFramework (robot, tsv) Ruby (rake, rb) Ruby HTML (rhtml) diff --git a/Unix/t/00_C.t b/Unix/t/00_C.t index b0a4bde5..dc243a09 100755 --- a/Unix/t/00_C.t +++ b/Unix/t/00_C.t @@ -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', diff --git a/cloc b/cloc index f3056232..17c1a247 100755 --- a/cloc +++ b/cloc @@ -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) @@ -6434,6 +6456,7 @@ sub set_constants { # {{{1 'psm1' => 'PowerShell' , 'R' => 'R' , 'r' => 'R' , + 'Rmd' => 'Rmd' , 'raml' => 'RAML' , 'rkt' => 'Racket' , 'rktl' => 'Racket' , @@ -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' , ';.*$' ], @@ -7592,6 +7620,7 @@ sub set_constants { # {{{1 'PowerShell' => '\\\\$' , 'Python' => '\\\\$' , 'R' => '\\\\$' , + 'Rmd' => '\\\\$' , 'Ruby' => '\\\\$' , 'sed' => '\\\\$' , 'Swift' => '\\\\$' , @@ -7686,6 +7715,7 @@ sub set_constants { # {{{1 'CHANGES' => 1, 'COPYING' => 1, 'COPYING' => 1, + 'DESCRIPTION' => 1, # R pacakges metafile '.cvsignore' => 1, 'Entries' => 1, 'FAQ' => 1, @@ -7693,6 +7723,7 @@ sub set_constants { # {{{1 'INSTALL' => 1, 'MAINTAINERS' => 1, 'MD5SUMS' => 1, + 'NAMESPACE' => 1, # R pacakges metafile 'NEWS' => 1, 'readme' => 1, 'Readme' => 1, @@ -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, From 9bc2c5aafe98836db9b18bab4581861797590958 Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sat, 19 May 2018 10:26:25 -0400 Subject: [PATCH 2/4] fixed 'package' spelling and figured out why test.Rmd[.yaml] weren't being added --- cloc | 4 ++-- tests/inputs/test.Rmd | 33 +++++++++++++++++++++++++++++++++ tests/outputs/test.Rmd.yaml | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/inputs/test.Rmd create mode 100644 tests/outputs/test.Rmd.yaml diff --git a/cloc b/cloc index 17c1a247..558b5079 100755 --- a/cloc +++ b/cloc @@ -7715,7 +7715,7 @@ sub set_constants { # {{{1 'CHANGES' => 1, 'COPYING' => 1, 'COPYING' => 1, - 'DESCRIPTION' => 1, # R pacakges metafile + 'DESCRIPTION' => 1, # R packages metafile '.cvsignore' => 1, 'Entries' => 1, 'FAQ' => 1, @@ -7723,7 +7723,7 @@ sub set_constants { # {{{1 'INSTALL' => 1, 'MAINTAINERS' => 1, 'MD5SUMS' => 1, - 'NAMESPACE' => 1, # R pacakges metafile + 'NAMESPACE' => 1, # R packages metafile 'NEWS' => 1, 'readme' => 1, 'Readme' => 1, diff --git a/tests/inputs/test.Rmd b/tests/inputs/test.Rmd new file mode 100644 index 00000000..41bce875 --- /dev/null +++ b/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 . + +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. diff --git a/tests/outputs/test.Rmd.yaml b/tests/outputs/test.Rmd.yaml new file mode 100644 index 00000000..8ba5af28 --- /dev/null +++ b/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 From fc281b23c35bbdb4853c70682b93a3a26989224b Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sat, 19 May 2018 10:33:51 -0400 Subject: [PATCH 3/4] replaced escape for left braece --- cloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloc b/cloc index 558b5079..b461be5f 100755 --- a/cloc +++ b/cloc @@ -5390,7 +5390,7 @@ sub reduce_to_rmd_code_blocks { #{{{1 my $in_code_block = 0; my @save_lines = (); foreach (@{$ra_lines}) { - if ( m/^```{\s*[[:alpha:]] / ) { + if ( m/^```\{\s*[[:alpha:]] / ) { $in_code_block = 1; next; } From 2f611c312e08f9a7ea52e31b751207ccd62f386b Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sat, 19 May 2018 10:40:52 -0400 Subject: [PATCH 4/4] proper intro Rmd code block regex --- cloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloc b/cloc index b461be5f..36d85ca1 100755 --- a/cloc +++ b/cloc @@ -5390,7 +5390,7 @@ sub reduce_to_rmd_code_blocks { #{{{1 my $in_code_block = 0; my @save_lines = (); foreach (@{$ra_lines}) { - if ( m/^```\{\s*[[:alpha:]] / ) { + if ( m/^```\{\s*[[:alpha:]]/ ) { $in_code_block = 1; next; }