Created
January 10, 2011 07:49
-
-
Save cameronbracken/772509 to your computer and use it in GitHub Desktop.
Break up long comments to multiple lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Do not need to load this library but need it installed | |
# library(tm) | |
library(stringr) | |
# get dummy text | |
loremipsum <- system.file("texts", "loremipsum.txt", package = "tm") | |
#A really long single line comment | |
comment <- paste('#', paste(readLines(loremipsum),collapse='')) | |
break.comment <- function(x, width=getOption('width')){ | |
loc <- last.blank.loc <- last.break <- 1 | |
# move through each character one by one... | |
while(loc < nchar(x)){ | |
# save the location if the current character can be broken on | |
if( !is.na( str_match( str_sub( x, loc, loc ),'[[:blank:]]' ) ) ) | |
last.blank.loc <- loc | |
# check if we hit the end of our line width | |
if((loc - last.break + 1) == width){ | |
# break the line at the last blank space | |
last.blank.character <- str_sub(x, last.blank.loc, last.blank.loc) | |
# break the line | |
str_sub(x, last.blank.loc, last.blank.loc) <- | |
paste(last.blank.character,'\n# ',sep='') | |
# reset last break location | |
last.break <- last.blank.loc + 1 | |
# added three characters | |
last.blank.loc <- last.blank.loc + 3 | |
loc <- last.blank.loc | |
} | |
# advance next character | |
loc <- loc + 1 | |
} | |
x | |
} | |
cat(break.comment( comment )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment