|
1 | 1 | /** |
2 | 2 | * MIT License (MIT) |
3 | 3 | * |
4 | | - * Copyright (c) 2014 - 2016 Volker Berlin |
| 4 | + * Copyright (c) 2014 - 2019 Volker Berlin |
5 | 5 | * |
6 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | 7 | * of this software and associated documentation files (the "Software"), to deal |
|
32 | 32 | import java.net.URL; |
33 | 33 | import java.nio.charset.StandardCharsets; |
34 | 34 | import java.nio.file.Files; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.Map; |
35 | 37 |
|
36 | 38 | /** |
37 | 39 | * The main class of JLessC library. Its contain all start points for converting LESS to CSS files. |
38 | 40 | */ |
39 | 41 | public class Less { |
40 | 42 |
|
| 43 | + /** |
| 44 | + * Key for compress option. true, if the CSS data should be compressed without any extra formating characters. |
| 45 | + */ |
| 46 | + public static final String COMPRESS = "compress"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Rewrites URLs to make them relative to the base less file. 'all' rewrites all URLs, 'local' just those starting |
| 50 | + * with a '.', 'off' simple inline it. |
| 51 | + */ |
| 52 | + public static final String REWRITE_URLS = "rewrite-urls"; |
| 53 | + |
41 | 54 | /** |
42 | 55 | * Compile the less data from a string. |
43 | 56 | * |
@@ -71,14 +84,55 @@ public static String compile( URL baseURL, String lessData, boolean compress ) t |
71 | 84 | * if any error occur on compiling. |
72 | 85 | */ |
73 | 86 | public static String compile( URL baseURL, String lessData, boolean compress, ReaderFactory readerFactory ) throws LessException { |
| 87 | + Map<String,String> params = Collections.singletonMap( COMPRESS, Boolean.toString( compress ) ); |
| 88 | + return compile( baseURL, lessData, params, readerFactory ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Compile the less data from a string. |
| 93 | + * |
| 94 | + * @param baseURL |
| 95 | + * the baseURL for import of external less data. |
| 96 | + * @param lessData |
| 97 | + * the input less data |
| 98 | + * @param options |
| 99 | + * some optional options, see constants for details |
| 100 | + * @return the resulting less data |
| 101 | + * @throws LessException |
| 102 | + * if any error occur on compiling. |
| 103 | + */ |
| 104 | + public static String compile( URL baseURL, String lessData, Map<String, String> options ) throws LessException { |
| 105 | + return compile( baseURL, lessData, options, new ReaderFactory() ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Compile the less data from a string. |
| 110 | + * |
| 111 | + * @param baseURL |
| 112 | + * the baseURL for import of external less data. |
| 113 | + * @param lessData |
| 114 | + * the input less data |
| 115 | + * @param options |
| 116 | + * some optional options, see constants for details |
| 117 | + * @param readerFactory |
| 118 | + * A factory for the readers for imports. |
| 119 | + * @return the resulting less data |
| 120 | + * @throws LessException |
| 121 | + * if any error occur on compiling. |
| 122 | + */ |
| 123 | + public static String compile( URL baseURL, String lessData, Map<String, String> options, ReaderFactory readerFactory ) throws LessException { |
74 | 124 | try { |
| 125 | + if( options == null ) { |
| 126 | + options = Collections.emptyMap(); |
| 127 | + } |
75 | 128 | LessParser parser = new LessParser(); |
76 | 129 | parser.parse( baseURL, new StringReader( lessData ), readerFactory ); |
77 | | - |
78 | | - StringBuilder builder = new StringBuilder(); |
79 | | - CssFormatter formatter = compress ? new CompressCssFormatter() : new CssFormatter(); |
| 130 | + |
| 131 | + boolean compress = Boolean.parseBoolean( options.get( COMPRESS ) ); |
| 132 | + CssFormatter formatter = compress ? new CompressCssFormatter() : new CssFormatter(); |
80 | 133 | parser.parseLazy( formatter ); |
81 | | - formatter.format( parser, baseURL, readerFactory, builder ); |
| 134 | + StringBuilder builder = new StringBuilder(); |
| 135 | + formatter.format( parser, baseURL, readerFactory, builder, options ); |
82 | 136 | return builder.toString(); |
83 | 137 | } catch( LessException ex ) { |
84 | 138 | throw ex; |
|
0 commit comments