ReadmeReader.java
/*
* Copyright © 2024-2025 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.services.texarchive.readme;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.ctan.site.services.texarchive.TexArchiveService.ReadmeTo;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* The class <code>ReadmeReader</code> contains the function to read a file and
* return the contents as HTML.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Getter
@AllArgsConstructor
public abstract class ReadmeReader {
/**
* The field <code>LIST</code> contains the ordered list of readme readers.
* The priority is highest for the first entry then decreased downwards.
*/
private static final ReadmeReader[] LIST = {
new TextReadmeReader("README"),
new MarkdownReadmeReader("README.md"),
new HtmlReadmeReader("index.html"),
new HtmlReadmeReader("index.htm"),
new HtmlReadmeReader("README.html"),
new HtmlReadmeReader("README.htm"),
new TextReadmeReader("README.txt"),
new TextReadmeReader("READ_ME")
};
/**
* The method <code>findIndex</code> provides means to retrieve a readme
* file according to a search list.
*
* @param dir the directory
* @return the transport object for the index
*/
public static ReadmeTo findIndex(File dir) {
for (ReadmeReader f : ReadmeReader.LIST) {
var file = new File(dir + "/" + f.getName());
if (file.isFile()) {
try {
return ReadmeTo.builder()
.name(f.getName())
.content(f.read(file))
.build();
} catch (IOException e) {
continue;
}
}
}
return null;
}
/**
* The field <code>name</code> contains the name of the file.
*/
private String name;
/**
* Read the contents.
*
* @param file the file to read
* @return the file contents as HTML
* @throws UnsupportedEncodingException in case of an error
* @throws IOException in case of an I/O error
*/
public abstract String read(File file)
throws UnsupportedEncodingException,
IOException;
}