| | |
| | import com.keenwrite.dom.DocumentParser; |
| | -import com.keenwrite.preferences.Workspace; |
| | import com.keenwrite.ui.heuristics.WordCounter; |
| | import com.whitemagicsoftware.keenquotes.Contractions; |
| | import com.whitemagicsoftware.keenquotes.Converter; |
| | -import javafx.beans.property.ListProperty; |
| | import org.w3c.dom.Document; |
| | |
| | import java.io.FileNotFoundException; |
| | import java.nio.file.Path; |
| | import java.util.LinkedHashMap; |
| | import java.util.List; |
| | import java.util.Locale; |
| | import java.util.Map; |
| | -import java.util.Map.Entry; |
| | -import java.util.regex.Pattern; |
| | |
| | import static com.keenwrite.Bootstrap.APP_TITLE_LOWERCASE; |
| | import static com.keenwrite.dom.DocumentParser.createMeta; |
| | import static com.keenwrite.dom.DocumentParser.visit; |
| | import static com.keenwrite.events.StatusEvent.clue; |
| | import static com.keenwrite.io.HttpFacade.httpGet; |
| | -import static com.keenwrite.preferences.AppKeys.*; |
| | import static com.keenwrite.util.ProtocolScheme.getProtocol; |
| | import static com.whitemagicsoftware.keenquotes.Converter.CHARS; |
| | import static com.whitemagicsoftware.keenquotes.ParserFactory.ParserType.PARSER_XML; |
| | import static java.lang.String.format; |
| | import static java.lang.String.valueOf; |
| | import static java.nio.file.Files.copy; |
| | import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| | -import static java.util.regex.Pattern.UNICODE_CHARACTER_CLASS; |
| | -import static java.util.regex.Pattern.compile; |
| | |
| | /** |
| | * Responsible for making an XHTML document complete by wrapping it with html |
| | * and body elements. This doesn't have to be super-efficient because it's |
| | * not run in real-time. |
| | */ |
| | public final class XhtmlProcessor extends ExecutorProcessor<String> { |
| | - private final static Pattern BLANK = |
| | - compile( "\\p{Blank}", UNICODE_CHARACTER_CLASS ); |
| | - |
| | private final static Converter sTypographer = new Converter( |
| | lex -> clue( lex.toString() ), contractions(), CHARS, PARSER_XML ); |
 |
| | |
| | final var document = DocumentParser.toString( doc ); |
| | + final var curl = mContext.getCurlQuotes(); |
| | |
| | - return curl() ? sTypographer.apply( document ) : document; |
| | + return curl ? sTypographer.apply( document ) : document; |
| | } catch( final Exception ex ) { |
| | clue( ex ); |
 |
| | */ |
| | private Map<String, String> createMetaDataMap( final Document doc ) { |
| | - final Map<String, String> result = new LinkedHashMap<>(); |
| | - final var metadata = getMetaData(); |
| | + final var result = new LinkedHashMap<String, String>(); |
| | + final var metadata = getMetadata(); |
| | final var map = mContext.getInterpolatedDefinitions(); |
| | |
| | - metadata.forEach( entry -> result.put( |
| | - entry.getKey(), map.interpolate( entry.getValue() ) ) |
| | + metadata.forEach( |
| | + ( key, value ) -> result.put( key, map.interpolate( value ) ) |
| | ); |
| | result.put( "count", wordCount( doc ) ); |
 |
| | * @return The document metadata. |
| | */ |
| | - private ListProperty<Entry<String, String>> getMetaData() { |
| | - return getWorkspace().listsProperty( KEY_DOC_META ); |
| | + private Map<String, String> getMetadata() { |
| | + return mContext.getMetadata(); |
| | } |
| | |
 |
| | } |
| | else { |
| | - final var extensions = " " + getImageOrder().trim(); |
| | + final var extensions = getImageOrder(); |
| | var imagePath = getImagePath(); |
| | var found = false; |
| | |
| | - // By including " " in the extensions, the first element returned |
| | - // will be the empty string. Thus the first extension to try is the |
| | - // file's default extension. Subsequent iterations will try to find |
| | - // a file that has a name matching one of the preferred extensions. |
| | - for( final var extension : BLANK.split( extensions ) ) { |
| | + for( final var extension : extensions ) { |
| | final var filename = format( |
| | "%s%s%s", src, extension.isBlank() ? "" : ".", extension ); |
 |
| | |
| | private String getImagePath() { |
| | - return getWorkspace().getFile( KEY_IMAGES_DIR ).toString(); |
| | + return mContext.getImageDir().toString(); |
| | } |
| | |
| | - private String getImageOrder() { |
| | - return getWorkspace().getString( KEY_IMAGES_ORDER ); |
| | + /** |
| | + * By including an "empty" extension, the first element returned |
| | + * will be the empty string. Thus, the first extension to try is the |
| | + * file's default extension. Subsequent iterations will try to find |
| | + * a file that has a name matching one of the preferred extensions. |
| | + * |
| | + * @return A list of extensions, including an empty string at the start. |
| | + */ |
| | + private Iterable<String> getImageOrder() { |
| | + return mContext.getImageOrder(); |
| | } |
| | |
 |
| | } |
| | |
| | - private Workspace getWorkspace() { |
| | - return mContext.getWorkspace(); |
| | + private Locale getLocale() { |
| | + return mContext.getLocale(); |
| | } |
| | - |
| | - private Locale locale() {return getWorkspace().getLocale();} |
| | |
| | private String wordCount( final Document doc ) { |
| | final var sb = new StringBuilder( 65536 * 10 ); |
| | |
| | visit( |
| | doc, |
| | "//*[normalize-space( text() ) != '']", |
| | node -> sb.append( node.getTextContent() ) |
| | ); |
| | - |
| | - return valueOf( WordCounter.create( locale() ).count( sb.toString() ) ); |
| | - } |
| | |
| | - /** |
| | - * Answers whether straight quotation marks should be curled. |
| | - * |
| | - * @return {@code false} to prevent curling straight quotes. |
| | - */ |
| | - private boolean curl() { |
| | - return getWorkspace().getBoolean( KEY_TYPESET_TYPOGRAPHY_QUOTES ); |
| | + return valueOf( WordCounter.create( getLocale() ).count( sb.toString() ) ); |
| | } |
| | |