public final class StyleWriter extends Object implements DependentPlugin
To use, add an instance of this class via Omakase.Request.use(Plugin...)
.
Examples:
StyleWriter verbose = StyleWriter.verbose();
Omakase.source(input).use(verbose).process();
String css = verbose.write();
StyleWriter compressed = StyleWriter.compressed();
Omakase.source(input).use(compressed).process();
String css = compressed.write();
StyleWriter verbose = StyleWriter.verbose();
Omakase.source(input).use(verbose).process();
verbose.writeTo(System.out);
String classSelector = StyleWriter.inline().writeSingle(new ClassSelector("test"));
Unless otherwise specified, WriterMode.INLINE
will be used.
By default this will not write out CSS comments, however you can change that behavior with writeAllComments(boolean)
.
Constructor and Description |
---|
StyleWriter()
Creates a new
StyleWriter instance using WriterMode.INLINE . |
StyleWriter(WriterMode mode)
Creates a new
StyleWriter instance using the given WriterMode . |
Modifier and Type | Method and Description |
---|---|
<T extends Writable> |
addCustomWriter(Class<T> writable,
CustomWriter<T> writer)
Overrides the writing of a unit with the given
CustomWriter instance. |
void |
appendComments(Iterable<Comment> comments,
StyleAppendable appendable)
Utility method for assisting with writing comments.
|
static StyleWriter |
compressed()
Creates a new
StyleWriter with WriterMode.COMPRESSED mode. |
int |
countAtCurrentDepth()
Gets the count of the number of peer units previously written out at the current depth level.
|
StyleWriter |
decrementDepth()
Decrements the stack depth.
|
void |
dependencies(PluginRegistry registry)
Registers plugin dependencies.
|
StyleWriter |
incrementDepth()
Increments the stack depth.
|
static StyleWriter |
inline()
Creates a new
StyleWriter with WriterMode.INLINE mode. |
boolean |
isCompressed()
Gets whether the current
WriterMode is WriterMode.COMPRESSED . |
boolean |
isFirstAtCurrentDepth()
Gets whether no peer units have been written out at the current depth level.
|
boolean |
isInline()
Gets whether the current
WriterMode is WriterMode.INLINE . |
boolean |
isVerbose()
Gets whether the current
WriterMode is WriterMode.VERBOSE . |
WriterMode |
mode()
Gets the
WriterMode . |
StyleWriter |
mode(WriterMode mode)
Sets the
WriterMode . |
boolean |
shouldWriteAllComments()
Gets whether all comments will be written out.
|
boolean |
shouldWriteAnnotatedComments()
Returns whether comments with annotations should be written out, even if
shouldWriteAllComments() is false. |
boolean |
shouldWriteBangComments()
Returns whether comments starting with '!' should be written out, even if
shouldWriteAllComments() is false. |
String |
toString() |
static StyleWriter |
verbose()
Creates a new
StyleWriter with WriterMode.VERBOSE mode. |
String |
write()
Writes the entire processed stylesheet to a string.
|
StyleWriter |
writeAllComments(boolean writeComments)
Sets whether all comments will be written out.
|
StyleWriter |
writeAnnotatedComments(boolean writeAnnotatedComments)
Sets whether comments with annotations should be written out, even if
shouldWriteAllComments() is false. |
StyleWriter |
writeBangComments(boolean writeBangComments)
Returns whether comments starting with '!' should be written out, even if
shouldWriteAllComments() is false. |
<T extends Writable> |
writeInner(T writable,
StyleAppendable appendable)
Writes the given syntax unit to the given
StyleAppendable , taking into account any CustomWriter overrides
specified on this StyleWriter . |
<T extends Writable> |
writeInner(T writable,
StyleAppendable appendable,
boolean useOverrides)
Writes the given syntax unit to the given
StyleAppendable . |
String |
writeSingle(Writable writable)
The easiest way to get the output of a single
Writable instance. |
void |
writeTo(Appendable appendable)
Writes the entire processed stylesheet to the given
Appendable . |
public StyleWriter()
StyleWriter
instance using WriterMode.INLINE
.public StyleWriter(WriterMode mode)
StyleWriter
instance using the given WriterMode
.mode
- The WriterMode
to use.public void dependencies(PluginRegistry registry)
DependentPlugin
Any plugins you add to the registry in this method will be ordered before this plugin itself.
Keep in mind that only one instance of a plugin can be added in a single parsing operation. You can use PluginRegistry.require(Class)
, PluginRegistry.require(Class, Supplier)
and PluginRegistry.retrieve(Class)
to assist in scenarios where a plugin instance may have already been added.
Dependencies to include can range from refinement dependencies such as SelectorPlugin
and MediaPlugin
to
other custom plugins.
dependencies
in interface DependentPlugin
registry
- The PluginRegistry
instance.PluginRegistry.require(Class)
,
PluginRegistry.require(Class, Supplier)
,
PluginRegistry.retrieve(Class)
public StyleWriter mode(WriterMode mode)
WriterMode
.mode
- The new WriterMode
.public WriterMode mode()
WriterMode
.public boolean isVerbose()
WriterMode
is WriterMode.VERBOSE
.WriterMode
is verbose.public boolean isInline()
WriterMode
is WriterMode.INLINE
.WriterMode
is inline.public boolean isCompressed()
WriterMode
is WriterMode.COMPRESSED
.WriterMode
is compressed.public StyleWriter writeAllComments(boolean writeComments)
writeComments
- Whether all comments should be written out.public boolean shouldWriteAllComments()
public StyleWriter writeAnnotatedComments(boolean writeAnnotatedComments)
shouldWriteAllComments()
is false. This can
be useful to preserve annotations across parsing operations.writeAnnotatedComments
- Whether comments with annotations should be written out.public boolean shouldWriteAnnotatedComments()
shouldWriteAllComments()
is false. This
can be useful to preserve annotations across parsing operations.public StyleWriter writeBangComments(boolean writeBangComments)
shouldWriteAllComments()
is false. There
must not be any whitespace between the comment open and the bang. This may be useful for preserving copyrights.writeBangComments
- Whether comments with bangs should be written out.public boolean shouldWriteBangComments()
shouldWriteAllComments()
is false. There
must not be any whitespace between the comment open and the bang. This may be useful for preserving copyrights.public <T extends Writable> StyleWriter addCustomWriter(Class<T> writable, CustomWriter<T> writer)
CustomWriter
instance. See CustomWriter
for more details on
overriding.T
- The Type of unit being overridden.writable
- The class of the unit to override.writer
- The CustomWriter
override.public String write()
public void writeTo(Appendable appendable) throws IOException
Appendable
.appendable
- Write the processed CSS source code to this appendable.IOException
- If an I/O error occurs.public <T extends Writable> void writeInner(T writable, StyleAppendable appendable) throws IOException
StyleAppendable
, taking into account any CustomWriter
overrides
specified on this StyleWriter
.
Note that the unit will only be written if Writable.isWritable()
returns true. This may be false if the unit is
detached/destroyed, for example.
This is usually used within implementations of Writable.write(StyleWriter, StyleAppendable)
to write inner units.
To write a single, arbitrary syntax unit use writeSingle(Writable)
instead. Do not call this method from
within a CustomWriter
.
This will automatically handle writing out comments and orphaned comments if the given writable is a Syntax
unit,
if applicable according to the current options.
T
- Type of the unit to write.writable
- The unit to write.appendable
- Write the unit's output to this StyleAppendable
.IOException
- If an I/O error occurs.public <T extends Writable> void writeInner(T writable, StyleAppendable appendable, boolean useOverrides) throws IOException
StyleAppendable
.
Note that the unit will only be written if Writable.isWritable()
returns true. This may be false if the unit is
detached/destroyed, for example.
This method can be used by CustomWriter
s to write out the unit as normal. To write a single, isolated object use
writeSingle(Writable)
instead.
This will automatically handle writing out comments and orphaned comments if the given writable is a Syntax
unit,
if applicable according to the current options.
T
- Type of the unit to write.writable
- The unit to write.appendable
- Write the unit's output to this StyleAppendable
.useOverrides
- If true, custom writers can alter the output behavior. Must always be false if calling this method from a custom
writer.IOException
- If an I/O error occurs.public String writeSingle(Writable writable)
Writable
instance.
This method is for writing disjoint units only. Examples would be for usage in test classes or in cases where you are operating on a single, isolated CSS unit as opposed to a whole CSS source.
If you are implementing Writable.write(StyleWriter, StyleAppendable)
then most of the time the method you want to
use is writeInner(Writable, StyleAppendable)
, passing in the same StyleAppendable
that you were given.
public void appendComments(Iterable<Comment> comments, StyleAppendable appendable) throws IOException
comments
- The comments to write.appendable
- Write the unit's output to this StyleAppendable
.IOException
- If an I/O error occurs.Syntax.writesOwnComments()
,
Syntax.writesOwnOrphanedComments()
public StyleWriter incrementDepth()
This is usually handled automatically internally, however you may want to call this when writing out inner objects at an
artificially deeper depth level. If you do so, be sure to call decrementDepth()
at the end.
public StyleWriter decrementDepth()
This is usually handled automatically internally. See incrementDepth()
for details on when you may want to use
this manually.
public int countAtCurrentDepth()
Writable.isWritable()
are not included.public boolean isFirstAtCurrentDepth()
The depth level is incremented every time writeInner(Writable, StyleAppendable)
inner is called. For example, two
rules within a stylesheet are at the same depth level, two selectors within a rule, two declarations within a rule, etc...
This is usually used when something should be written only for the first unit in a collection, or conversely for all units but the first.
public static StyleWriter verbose()
StyleWriter
with WriterMode.VERBOSE
mode.StyleWriter
instance.public static StyleWriter inline()
StyleWriter
with WriterMode.INLINE
mode.StyleWriter
instance.public static StyleWriter compressed()
StyleWriter
with WriterMode.COMPRESSED
mode.StyleWriter
instance.Copyright (c) 2019, Salesforce.com, Inc. All rights reserved. Licensed under the BSD 3-Clause license. For full license text, see the LICENSE file in the repository.