| | |
| | import com.scrivenvar.Services; |
| | +import com.scrivenvar.preferences.UserPreferences; |
| | import com.scrivenvar.service.Options; |
| | +import com.scrivenvar.service.events.Notifier; |
| | import com.vladsch.flexmark.ast.Image; |
| | import com.vladsch.flexmark.html.HtmlRenderer; |
 |
| | import com.vladsch.flexmark.util.data.MutableDataHolder; |
| | import org.jetbrains.annotations.NotNull; |
| | +import org.renjin.repackaged.guava.base.Splitter; |
| | |
| | -import java.util.prefs.Preferences; |
| | +import java.io.File; |
| | |
| | -import static com.scrivenvar.Constants.PERSIST_IMAGES_DIRECTORY; |
| | +import static java.lang.String.format; |
| | |
| | /** |
| | * Responsible for ensuring that images can be rendered relative to a path. |
| | * This allows images to be located virtually anywhere. |
| | * |
| | * @author White Magic Software, Ltd. |
| | */ |
| | public class ImageLinkExtension implements HtmlRenderer.HtmlRendererExtension { |
| | - private final Options mOptions = Services.load( Options.class ); |
| | + private final static Options OPTIONS = Services.load( Options.class ); |
| | + private final static Notifier NOTIFIER = Services.load( Notifier.class ); |
| | |
| | public static ImageLinkExtension create() { |
 |
| | |
| | private class ImageLinkResolver implements LinkResolver { |
| | + private final UserPreferences mUserPref = getUserPreferences(); |
| | + private final String mImagePrefix = mUserPref.getImagesDirectory() |
| | + .toString(); |
| | + private final String mImageuffixes = mUserPref.getImagesOrder(); |
| | + |
| | public ImageLinkResolver() { |
| | } |
 |
| | private ResolvedLink resolve( |
| | @NotNull final Image image, @NotNull final ResolvedLink link ) { |
| | - final String url = link.getUrl(); |
| | + String url = link.getUrl(); |
| | |
| | try { |
| | - final Preferences p = getPreferences(); |
| | - final String prefix = p.get( PERSIST_IMAGES_DIRECTORY, "" ); |
| | - |
| | - System.out.printf( "prefix = %s%n", prefix ); |
| | - |
| | - //URI uri = new URI( url ); |
| | + final String filename = format( "%s/%s", getImagePrefix(), url ); |
| | + final String suffixes = getImageSuffixes(); |
| | |
| | -// System.out.println( "image: " + image.toString() ); |
| | -// System.out.println( "Absolute: " + uri.isAbsolute() ); |
| | -// System.out.println( "Host: " + uri.getHost() ); |
| | + for( final String ext : Splitter.on( ' ' ).split( suffixes ) ) { |
| | + final File file = new File( format( "%s.%s", filename, ext ) ); |
| | |
| | - final String imageUrl = link.getUrl(); |
| | + if( file.exists() ) { |
| | + url = file.toString(); |
| | + break; |
| | + } |
| | + } |
| | |
| | - return link.withStatus( LinkStatus.VALID ) |
| | - .withUrl( imageUrl ); |
| | + System.out.println( "URL: " + url ); |
| | |
| | + return link.withStatus( LinkStatus.VALID ).withUrl( url ); |
| | } catch( final Exception e ) { |
| | - System.out.println( "Bad URI: " + url ); |
| | + getNotifier().notify( e ); |
| | } |
| | |
| | return link; |
| | + } |
| | + |
| | + private String getImagePrefix() { |
| | + return mImagePrefix; |
| | + } |
| | + |
| | + private String getImageSuffixes() { |
| | + return mImageuffixes; |
| | } |
| | } |
 |
| | } |
| | |
| | - private Preferences getPreferences() { |
| | - return getOptions().getState(); |
| | + private UserPreferences getUserPreferences() { |
| | + return getOptions().getUserPreferences(); |
| | } |
| | |
| | private Options getOptions() { |
| | - return mOptions; |
| | + return OPTIONS; |
| | + } |
| | + |
| | + private Notifier getNotifier() { |
| | + return NOTIFIER; |
| | } |
| | } |