From 66360146d7ac32d6bb650c0b7da98c4417e4ea92 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Thu, 30 Jan 2020 11:50:40 +0100 Subject: [PATCH] Refactored ListStyleParser css declarations --- .../xhtml/style/ListStyleParser.java | 94 ++++++++++++------- 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/src/main/java/writer2latex/xhtml/style/ListStyleParser.java b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java index 7bd8295..166de53 100644 --- a/src/main/java/writer2latex/xhtml/style/ListStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java @@ -97,43 +97,71 @@ public class ListStyleParser extends StyleParser { StringBuilder buf = new StringBuilder(); Enumeration names = styleNames.keys(); while (names.hasMoreElements()) { - String sDisplayName = names.nextElement(); - ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(sDisplayName); - if (!style.isAutomatic()) { - for (int nLevel = 1; nLevel < 10; nLevel++) { - CSVList props = new CSVList(";"); - cssList(style, nLevel, props); - buf.append(indent); - buf.append(".listlevel"); - buf.append(nLevel); - styleNames.addName(sDisplayName); - buf.append(styleNames.getName(sDisplayName)); - buf.append(" {"); - buf.append(props.toString()); - buf.append("}"); - buf.append(prettyPrint ? "\n" : " "); - if (config.listFormatting() == XhtmlConfig.HARD_LABELS) { - // Apply left margin and text indent to the paragraphs contained in - // the list - CSVList parProps = new CSVList(";"); - cssListParMargins(style, nLevel, parProps); - if (!parProps.isEmpty()) { - buf.append(indent); - buf.append(".listlevel"); - buf.append(nLevel); - styleNames.addName(sDisplayName); - buf.append(styleNames.getName(sDisplayName)); - buf.append(" p {"); - buf.append(parProps.toString()); - buf.append("}"); - buf.append(prettyPrint ? "\n" : " "); - } - } + String displayName = names.nextElement(); + buf.append(composeDeclaration(displayName)); + if (config.listFormatting() == XhtmlConfig.HARD_LABELS) { + buf.append(composeHardDeclaration(displayName)); + } + } + return buf.toString(); + } + public String composeDeclaration(String displayName) { + StringBuilder buf = new StringBuilder(); + ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(displayName); + if (!style.isAutomatic()) { + for (int level = 1; level < 10; level++) { + String selector = composeSelector(displayName, level); + String declarationBlock = composeDeclarationBlock(displayName, level); + if (!selector.isEmpty() && !declarationBlock.isEmpty()) { + buf.append(indent + selector + " {" + declarationBlock + "}" + (prettyPrint ? "\n" : " ")); } } } return buf.toString(); - } + } + + public String composeHardDeclaration(String displayName) { + StringBuilder buf = new StringBuilder(); + ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(displayName); + if (!style.isAutomatic()) { + for (int level = 1; level < 10; level++) { + String declarationBlock = composeHardDeclarationBlock(displayName, level); + String selector = composeHardSelector(displayName, level); + // Apply left margin and text indent to the paragraphs contained in + // the list + if (!declarationBlock.isEmpty() && !selector.isEmpty()) { + buf.append(indent + selector + "{" + declarationBlock + "}" + (prettyPrint ? "\n" : " ")); + } + } + } + return buf.toString(); + } + + private String composeHardSelector(String displayName, int level) { + styleNames.addName(displayName); + return ".listlevel" + level + styleNames.getName(displayName) + " p "; + } + + private String composeSelector(String displayName, int level) { + styleNames.addName(displayName); + return ".listlevel" + level + styleNames.getName(displayName); + } + + private String composeHardDeclarationBlock(String displayName, int level) { + ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(displayName); + CSVList props = new CSVList(";"); + cssListParMargins(style, level, props); + return props.toString(); + } + + private String composeDeclarationBlock(String displayName, int level) { + ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(displayName); + CSVList props = new CSVList(";"); + cssList(style, level, props); + return props.toString(); + } + + /** Get the family of list styles * @return the style family