Refactored extensions related into static ExtensionsUtil.
This commit is contained in:
parent
0371d41ccc
commit
99836856d7
@ -1,13 +1,14 @@
|
|||||||
package com.jessebrault.ssg.part
|
package com.jessebrault.ssg.part
|
||||||
|
|
||||||
import com.jessebrault.ssg.provider.WithWatchableDir
|
import com.jessebrault.ssg.provider.WithWatchableDir
|
||||||
import com.jessebrault.ssg.util.FileNameHandler
|
|
||||||
import groovy.io.FileType
|
import groovy.io.FileType
|
||||||
import groovy.transform.EqualsAndHashCode
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.transform.NullCheck
|
import groovy.transform.NullCheck
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.getExtension
|
||||||
|
|
||||||
@NullCheck
|
@NullCheck
|
||||||
@EqualsAndHashCode(includeFields = true)
|
@EqualsAndHashCode(includeFields = true)
|
||||||
class PartFilePartsProvider implements PartsProvider, WithWatchableDir {
|
class PartFilePartsProvider implements PartsProvider, WithWatchableDir {
|
||||||
@ -24,8 +25,9 @@ class PartFilePartsProvider implements PartsProvider, WithWatchableDir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PartType getPartType(File file) {
|
private PartType getPartType(File file) {
|
||||||
|
def path = file.path
|
||||||
this.partTypes.find {
|
this.partTypes.find {
|
||||||
it.ids.contains(new FileNameHandler(file).getExtension())
|
it.ids.contains(getExtension(path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package com.jessebrault.ssg.specialpage
|
package com.jessebrault.ssg.specialpage
|
||||||
|
|
||||||
import com.jessebrault.ssg.provider.WithWatchableDir
|
import com.jessebrault.ssg.provider.WithWatchableDir
|
||||||
import com.jessebrault.ssg.util.FileNameHandler
|
|
||||||
import com.jessebrault.ssg.util.RelativePathHandler
|
|
||||||
import groovy.io.FileType
|
import groovy.io.FileType
|
||||||
import groovy.transform.EqualsAndHashCode
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.transform.NullCheck
|
import groovy.transform.NullCheck
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.getExtension
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.stripExtension
|
||||||
|
|
||||||
@NullCheck
|
@NullCheck
|
||||||
@EqualsAndHashCode(includeFields = true)
|
@EqualsAndHashCode(includeFields = true)
|
||||||
class SpecialPageFileSpecialPagesProvider implements SpecialPagesProvider, WithWatchableDir {
|
class SpecialPageFileSpecialPagesProvider implements SpecialPagesProvider, WithWatchableDir {
|
||||||
@ -25,8 +26,9 @@ class SpecialPageFileSpecialPagesProvider implements SpecialPagesProvider, WithW
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SpecialPageType getSpecialPageType(File file) {
|
private SpecialPageType getSpecialPageType(File file) {
|
||||||
|
def path = file.path
|
||||||
this.specialPageTypes.find {
|
this.specialPageTypes.find {
|
||||||
it.ids.contains(new FileNameHandler(file).getExtension())
|
it.ids.contains(getExtension(path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +43,7 @@ class SpecialPageFileSpecialPagesProvider implements SpecialPagesProvider, WithW
|
|||||||
def type = this.getSpecialPageType(it)
|
def type = this.getSpecialPageType(it)
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
def relativePath = this.specialPagesDir.relativePath(it)
|
def relativePath = this.specialPagesDir.relativePath(it)
|
||||||
def path = new RelativePathHandler(relativePath).getWithoutExtension()
|
def path = stripExtension(relativePath)
|
||||||
logger.info('found specialPage {} with type {}', path, type)
|
logger.info('found specialPage {} with type {}', path, type)
|
||||||
specialPages << new SpecialPage(it.text, path, type)
|
specialPages << new SpecialPage(it.text, path, type)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package com.jessebrault.ssg.template
|
package com.jessebrault.ssg.template
|
||||||
|
|
||||||
import com.jessebrault.ssg.provider.WithWatchableDir
|
import com.jessebrault.ssg.provider.WithWatchableDir
|
||||||
import com.jessebrault.ssg.util.FileNameHandler
|
|
||||||
import groovy.io.FileType
|
import groovy.io.FileType
|
||||||
import groovy.transform.EqualsAndHashCode
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.transform.NullCheck
|
import groovy.transform.NullCheck
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.getExtension
|
||||||
|
|
||||||
@NullCheck
|
@NullCheck
|
||||||
@EqualsAndHashCode(includeFields = true)
|
@EqualsAndHashCode(includeFields = true)
|
||||||
class TemplateFileTemplatesProvider implements TemplatesProvider, WithWatchableDir {
|
class TemplateFileTemplatesProvider implements TemplatesProvider, WithWatchableDir {
|
||||||
@ -24,8 +25,9 @@ class TemplateFileTemplatesProvider implements TemplatesProvider, WithWatchableD
|
|||||||
}
|
}
|
||||||
|
|
||||||
private TemplateType getType(File file) {
|
private TemplateType getType(File file) {
|
||||||
|
def path = file.path
|
||||||
this.templateTypes.find {
|
this.templateTypes.find {
|
||||||
it.ids.contains(new FileNameHandler(file).getExtension())
|
it.ids.contains(getExtension(path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package com.jessebrault.ssg.text
|
package com.jessebrault.ssg.text
|
||||||
|
|
||||||
import com.jessebrault.ssg.provider.WithWatchableDir
|
import com.jessebrault.ssg.provider.WithWatchableDir
|
||||||
import com.jessebrault.ssg.util.FileNameHandler
|
|
||||||
import com.jessebrault.ssg.util.RelativePathHandler
|
|
||||||
import groovy.io.FileType
|
import groovy.io.FileType
|
||||||
import groovy.transform.EqualsAndHashCode
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.transform.NullCheck
|
import groovy.transform.NullCheck
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.getExtension
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.stripExtension
|
||||||
|
|
||||||
@NullCheck
|
@NullCheck
|
||||||
@EqualsAndHashCode(includeFields = true)
|
@EqualsAndHashCode(includeFields = true)
|
||||||
class TextFileTextsProvider implements TextsProvider, WithWatchableDir {
|
class TextFileTextsProvider implements TextsProvider, WithWatchableDir {
|
||||||
@ -25,8 +26,9 @@ class TextFileTextsProvider implements TextsProvider, WithWatchableDir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private TextType getTextType(File file) {
|
private TextType getTextType(File file) {
|
||||||
|
def path = file.path
|
||||||
this.textTypes.find {
|
this.textTypes.find {
|
||||||
it.ids.contains(new FileNameHandler(file).getExtension())
|
it.ids.contains(getExtension(path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +43,7 @@ class TextFileTextsProvider implements TextsProvider, WithWatchableDir {
|
|||||||
def type = this.getTextType(it)
|
def type = this.getTextType(it)
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
def relativePath = this.textsDir.relativePath(it)
|
def relativePath = this.textsDir.relativePath(it)
|
||||||
def path = new RelativePathHandler(relativePath).getWithoutExtension()
|
def path = stripExtension(relativePath)
|
||||||
logger.debug('found textFile {} with type {}', path, type)
|
logger.debug('found textFile {} with type {}', path, type)
|
||||||
textFiles << new Text(it.text, path, type)
|
textFiles << new Text(it.text, path, type)
|
||||||
} else {
|
} else {
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.jessebrault.ssg.util
|
||||||
|
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
class ExtensionsUtil {
|
||||||
|
|
||||||
|
private static final Pattern stripExtensionPattern = ~/(.+)\..+$/
|
||||||
|
private static final Pattern getExtensionPattern = ~/.+(\..+)$/
|
||||||
|
|
||||||
|
static String stripExtension(String path) {
|
||||||
|
def m = stripExtensionPattern.matcher(path)
|
||||||
|
m.matches() ? m.group(1) : path
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getExtension(String path) {
|
||||||
|
def m = getExtensionPattern.matcher(path)
|
||||||
|
if (m.matches()) {
|
||||||
|
m.group(1)
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("cannot get extension for path: ${ path }")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,32 +0,0 @@
|
|||||||
package com.jessebrault.ssg.util
|
|
||||||
|
|
||||||
import groovy.transform.EqualsAndHashCode
|
|
||||||
import groovy.transform.NullCheck
|
|
||||||
import groovy.transform.TupleConstructor
|
|
||||||
|
|
||||||
@TupleConstructor(includeFields = true, defaults = false)
|
|
||||||
@NullCheck
|
|
||||||
@EqualsAndHashCode(includeFields = true)
|
|
||||||
class FileNameHandler {
|
|
||||||
|
|
||||||
private final File file
|
|
||||||
|
|
||||||
String getExtension() {
|
|
||||||
def lastIndexOfDot = this.file.name.lastIndexOf('.')
|
|
||||||
if (lastIndexOfDot == -1) {
|
|
||||||
''
|
|
||||||
} else {
|
|
||||||
this.file.name.substring(lastIndexOfDot)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String getWithoutExtension() {
|
|
||||||
this.file.name.substring(0, this.file.name.lastIndexOf('.'))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
String toString() {
|
|
||||||
"FileNameHandler(file: ${ this.file })"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package com.jessebrault.ssg.util
|
|
||||||
|
|
||||||
import groovy.transform.EqualsAndHashCode
|
|
||||||
import groovy.transform.NullCheck
|
|
||||||
import groovy.transform.TupleConstructor
|
|
||||||
|
|
||||||
@TupleConstructor(includeFields = true, defaults = false)
|
|
||||||
@NullCheck
|
|
||||||
@EqualsAndHashCode(includeFields = true)
|
|
||||||
class RelativePathHandler {
|
|
||||||
|
|
||||||
private final String relativePath
|
|
||||||
|
|
||||||
String getWithoutExtension() {
|
|
||||||
this.relativePath.subSequence(0, this.relativePath.lastIndexOf('.'))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
String toString() {
|
|
||||||
"RelativePathHandler(relativePath: ${ this.relativePath })"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.jessebrault.ssg.util
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.stripExtension
|
||||||
|
import static com.jessebrault.ssg.util.ExtensionsUtil.getExtension
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
|
||||||
|
class ExtensionsUtilTests {
|
||||||
|
|
||||||
|
static class StripExtensionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void simple() {
|
||||||
|
assertEquals('test', stripExtension('test.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withSlashes() {
|
||||||
|
assertEquals('test/test', stripExtension('test/test.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withMultipleExtensions() {
|
||||||
|
assertEquals('test.txt', stripExtension('test.txt.html'))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static class GetExtensionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void simple() {
|
||||||
|
assertEquals('.txt', getExtension('test.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withSlashes() {
|
||||||
|
assertEquals('.txt', getExtension('test/test.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withMultipleExtensions() {
|
||||||
|
assertEquals('.txt', getExtension('test.test.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
package com.jessebrault.ssg.util
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals
|
|
||||||
|
|
||||||
class FileNameHandlerTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getsCorrectExtension() {
|
|
||||||
def file = new File('hello.txt')
|
|
||||||
def extension = new FileNameHandler(file).getExtension()
|
|
||||||
assertEquals('.txt', extension)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package com.jessebrault.ssg.util
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals
|
|
||||||
|
|
||||||
class RelativePathHandlerTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void stripsExtension() {
|
|
||||||
def stripped = new RelativePathHandler('hello.txt').getWithoutExtension()
|
|
||||||
assertEquals('hello', stripped)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user