Skip to content

Commit

Permalink
Adding preserveLineBreaks setting. Fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
craigburke committed Nov 1, 2016
1 parent 64459a4 commit c7d39d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ assets {
angular : [
templateFolder: 'templates',
moduleNameBase: '',
includePathInName: true,
templateModuleName: '',
convertUnderscores: true,
compressHtml: true,
preserveHtmlComments: false,
includePathInName: true
preserveLineBreaks: false
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HTMLTemplateProcessor {
String moduleName = getModuleName(assetFile, config)

String templateName = getTemplateName(assetFile, config.templateFolder, config.includePathInName)
String content = formatHtml(input.toString(), config.compressHtml, config.preserveHtmlComments)
String content = formatHtml(input.toString(), config)

getTemplateJs(moduleName, templateName, content)
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/com/craigburke/angular/ProcessorConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class ProcessorConfig {
final String moduleBaseName
final Boolean convertUnderscores
final Boolean includePathInName

final Boolean compressHtml
final Boolean preserveHtmlComments
final Boolean preserveLineBreaks

ProcessorConfig(Map config = [:]) {
templateModuleName = config.containsKey('templateModuleName') ? config.templateModuleName : ''
Expand All @@ -28,6 +30,7 @@ class ProcessorConfig {
moduleBaseName = ''
}

preserveLineBreaks = config.containsKey('preserveLineBreaks') ? config.preserveLineBreaks : false
convertUnderscores = config.containsKey('convertUnderscores') ? config.convertUnderscores : true
includePathInName = config.containsKey('includePathInName') ? config.includePathInName : true
compressHtml = config.containsKey('compressHtml') ? config.compressHtml : true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,31 @@ import groovy.transform.CompileStatic
@CompileStatic
class TemplateProcessorUtil {

static String formatHtml(String html, boolean compressHtml, boolean preserveHtmlComments) {
if (compressHtml) {
HtmlCompressor compressor = new HtmlCompressor()

if (preserveHtmlComments) {
compressor.removeComments = false
static String formatHtml(String html, ProcessorConfig config) {

if (config.compressHtml) {
HtmlCompressor compressor = new HtmlCompressor(
removeComments: !config.preserveHtmlComments,
preserveLineBreaks: config.preserveLineBreaks
)
html = compressor.compress(html)
if (config.preserveLineBreaks) {
html = proccessLineBreaks(html, true)
}

html = compressor.compress html
}
else {
html = html.replaceAll("(\r)?\n", " \\n")
html = proccessLineBreaks(html, config.preserveLineBreaks)
}

html = html.replace("'", "\\'")
html
}

private static String proccessLineBreaks(String input, boolean preserve) {
String lineBreakReplacement = preserve ? " \\\\n" : ' '
input.replaceAll("(\r)?\n", lineBreakReplacement)
}

static String getFileNameFromPath(AssetFile file) {
List<String> pathParts = file.path.tokenize('/')
pathParts ? pathParts[-1] : ''
Expand Down

0 comments on commit c7d39d4

Please sign in to comment.