perak
perak

Reputation: 1340

Gradle/Java replace text

Is it possible to use regex to convert urls in css files in the way demonstrated below? I don't want to hardcode font names.

from:

url('../fonts/glyphiconshalflings-regular.eot?#iefix') format('embedded-opentype')

to:

url("#{resource['css:../fonts/glyphiconshalflings-regular.eot']}?#iefix") format('embedded-opentype')

And this is what I currently have:

task fixCSSResources << {
    FileTree cssFiles = fileTree("src/main/webapp") {
        include "**/*.css"
    }
    cssFiles.each { File cssFile ->
        String content = cssFile.getText("UTF-8")
        // what to do here?
        cssFile.write(content, "UTF-8")
    }
}

Upvotes: 3

Views: 5746

Answers (1)

Song Gao
Song Gao

Reputation: 666

Assuming you just want to format url(something), this should do what you want:

String regex = "url\\('([^']*?\\.(eot|ttf|fnt))(.*?)'\\)";
//                   font file formats^           
content = content.replaceAll(regex, "url(\"#{resource['css:$1']}$3\")");

EDIT: forgot to escape some characters.

Upvotes: 5

Related Questions