From b2c79c01386ff0cbe51c3cb8f4091b4535154307 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Thu, 30 Jan 2020 15:41:39 +0100 Subject: [PATCH] Refactored TextStyleParser css declarations --- .../xhtml/style/FrameStyleParser.java | 2 +- .../xhtml/style/HeadingStyleParser.java | 4 +- .../xhtml/style/ListStyleParser.java | 4 +- .../xhtml/style/PageStyleParser.java | 37 +++-- .../xhtml/style/PresentationStyleParser.java | 2 +- .../writer2latex/xhtml/style/StyleParser.java | 2 +- .../style/StyleWithPropertiesParser.java | 2 +- .../xhtml/style/TextStyleParser.java | 147 ++++++++++-------- 8 files changed, 116 insertions(+), 84 deletions(-) diff --git a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java index b08d443..28cd898 100644 --- a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java @@ -82,7 +82,7 @@ public class FrameStyleParser extends StyleWithPropertiesParser { String declarationBlock = composeDeclarationBlock(displayName); String selector = composeSelector(displayName); if (!style.isAutomatic() && !selector.isEmpty() && !declarationBlock.isEmpty()) { - declaration = cssToString(selector, declarationBlock); + declaration = createDeclaration(selector, declarationBlock); } return declaration; } diff --git a/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java b/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java index f18e426..20c4d57 100644 --- a/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java @@ -66,13 +66,13 @@ public class HeadingStyleParser extends StyleParser { if (ofr.getHeadingStyle(i) != null) { String selector = composeBasicSelector(i); String declaration = composeBasicDeclarationBlock(i); - result.append(cssToString(selector, declaration)); + result.append(createDeclaration(selector, declaration)); } // Convert other styles for this level for (String sDisplayName : otherLevelStyles.get(i)) { String selector = composeSelector(i, sDisplayName); String declaration = composeDeclarationBlock( sDisplayName); - result.append(cssToString(selector, declaration)); + result.append(createDeclaration(selector, declaration)); } } return result.toString(); diff --git a/src/main/java/writer2latex/xhtml/style/ListStyleParser.java b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java index 2458edd..1a11ba9 100644 --- a/src/main/java/writer2latex/xhtml/style/ListStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java @@ -113,7 +113,7 @@ public class ListStyleParser extends StyleParser { String selector = composeSelector(displayName, level); String declarationBlock = composeDeclarationBlock(displayName, level); if (!selector.isEmpty() && !declarationBlock.isEmpty()) { - buf.append(cssToString(selector, declarationBlock)); + buf.append(createDeclaration(selector, declarationBlock)); } } } @@ -130,7 +130,7 @@ public class ListStyleParser extends StyleParser { // Apply left margin and text indent to the paragraphs contained in // the list if (!declarationBlock.isEmpty() && !selector.isEmpty()) { - buf.append(cssToString(selector, declarationBlock)); + buf.append(createDeclaration(selector, declarationBlock)); } } } diff --git a/src/main/java/writer2latex/xhtml/style/PageStyleParser.java b/src/main/java/writer2latex/xhtml/style/PageStyleParser.java index c5b1212..bcf20a6 100644 --- a/src/main/java/writer2latex/xhtml/style/PageStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/PageStyleParser.java @@ -156,31 +156,44 @@ public class PageStyleParser extends StyleParser { if (pageLayout == null) { return ""; } // Background color - StyleInfo pageInfo = new StyleInfo(); - getFrameSc().cssBackground(pageLayout, pageInfo.props, true); - - if (pageInfo.hasAttributes()) { - buf.append(indent + "body" + " {" + pageInfo.props.toString() + "}" + (prettyPrint ? "\n" : " ")); + String pageDeclaration = composePageDeclaration(); + if (!pageDeclaration.isEmpty()) { + buf.append(createDeclaration("body", pageDeclaration)); } - // Footnote rule - if (hasFootnoteRules) { - StyleInfo ruleInfo = new StyleInfo(); - cssFootnoteRule(pageLayout, ruleInfo.props); - buf.append(indent + "hr.footnoterule {" + ruleInfo.props.toString() + "}" + (prettyPrint ? "\n" : " ")); - + String footnoteDeclaration = composeFootnoteDeclaration(); + if (hasFootnoteRules && !footnoteDeclaration.isEmpty()) { + buf.append(createDeclaration("hr.footnoterule", footnoteDeclaration)); } return buf.toString(); } + private String composePageDeclaration() { + MasterPage masterPage = ofr.getFirstMasterPage(); + PageLayout pageLayout = ofr.getPageLayout(masterPage.getPageLayoutName()); + StyleInfo pageInfo = new StyleInfo(); + getFrameSc().cssBackground(pageLayout, pageInfo.props, true); + String pageDeclaration = pageInfo.props.toString(); + return pageDeclaration; + } + + private String composeFootnoteDeclaration() { + MasterPage masterPage = ofr.getFirstMasterPage(); + PageLayout pageLayout = ofr.getPageLayout(masterPage.getPageLayoutName()); + StyleInfo ruleInfo = new StyleInfo(); + cssFootnoteRule(pageLayout, ruleInfo.props); + String footnoteRulesDeclaration = ruleInfo.props.toString(); + return footnoteRulesDeclaration; + } + private String composeDeclaration(String displayName) { String declaration = ""; String selector = composeSelector(displayName); String declarationBlock = composeDeclarationBlock(displayName); // Then export the results if (!selector.isEmpty() && !declarationBlock.isEmpty()) { - declaration = cssToString(selector, declarationBlock); + declaration = createDeclaration(selector, declarationBlock); } return declaration; } diff --git a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java index 8fb221c..38710bd 100644 --- a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java @@ -108,7 +108,7 @@ public class PresentationStyleParser extends FrameStyleParser { String selector = composeSelector(displayName);; if (!style.isAutomatic() && !selector.isEmpty() && !declarationBlock.isEmpty()) { // Apply style to paragraphs within a list item with this class - declaration = indent + selector + "{" + declarationBlock + "}" + (prettyPrint ? "\n" : " "); + declaration = createDeclaration(selector, declarationBlock); } return declaration; } diff --git a/src/main/java/writer2latex/xhtml/style/StyleParser.java b/src/main/java/writer2latex/xhtml/style/StyleParser.java index ce57308..05b99c8 100644 --- a/src/main/java/writer2latex/xhtml/style/StyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/StyleParser.java @@ -125,7 +125,7 @@ public abstract class StyleParser extends Parser { */ public abstract String composeStyleDeclarations(); - public String cssToString(String selector, String declarationBlock) { + public String createDeclaration(String selector, String declarationBlock) { return indent + selector + " {" + declarationBlock + "}" + (prettyPrint ? "\n" : " "); } diff --git a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java index 6b5ea70..756036c 100644 --- a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java +++ b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java @@ -114,7 +114,7 @@ public abstract class StyleWithPropertiesParser extends StyleParser { String selector = composeSelector(displayName); String declarationBlock = composeDeclarationBlock(displayName); if (!style.isAutomatic() && !selector.isEmpty() && !declarationBlock.isEmpty()) { - declaration = cssToString(selector, declarationBlock); + declaration = createDeclaration(selector, declarationBlock); // TODO: Create a method "getStyleDeclarationsInner" // to be used by eg. FrameStyleConverter } diff --git a/src/main/java/writer2latex/xhtml/style/TextStyleParser.java b/src/main/java/writer2latex/xhtml/style/TextStyleParser.java index 35c9934..8cce2f4 100644 --- a/src/main/java/writer2latex/xhtml/style/TextStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/TextStyleParser.java @@ -154,74 +154,93 @@ public class TextStyleParser extends StyleWithPropertiesParser { /**

Convert style information for used styles

* @param sIndent a String of spaces to add before each line */ - StringBuilder buf = new StringBuilder(); - public String composeStyleDeclarations() { - buf.append(super.composeStyleDeclarations()); - if (bConvertStyles) { - // Export anchor styles - // Default is always the styles "Internet link" and "Visited Internet Link"(?) - StyleWithProperties defaultLinkStyle = (StyleWithProperties) - getStyles().getStyleByDisplayName(DEFAULT_LINK_STYLE); - if (defaultLinkStyle!=null) { - CSVList props = new CSVList(";"); - cssText(defaultLinkStyle,props,true); - cssHyperlink(defaultLinkStyle,props); - buf.append(indent); - buf.append("a:link {"); - buf.append(props.toString()); - buf.append("}\n"); - } - - defaultLinkStyle = (StyleWithProperties) - getStyles().getStyleByDisplayName(DEFAULT_VISITED_LINK_STYLE); - if (defaultLinkStyle!=null) { - CSVList props = new CSVList(";"); - cssText(defaultLinkStyle,props,true); - cssHyperlink(defaultLinkStyle,props); - buf.append(indent); - buf.append("a:visited {"); - buf.append(props.toString()); - buf.append("}\n"); - } + public String composeStyleDeclarations() { + StringBuilder buf = new StringBuilder(); + buf.append(super.composeStyleDeclarations()); + if (!bConvertStyles) { + return ""; + } + // Export anchor styles + // Default is always the styles "Internet link" and "Visited Internet + // Link"(?) + StyleWithProperties defaultLinkStyle = (StyleWithProperties) getStyles().getStyleByDisplayName(DEFAULT_LINK_STYLE); + if (defaultLinkStyle != null) { + String linkDeclarationBlock = composeLinkDeclarationBlock(defaultLinkStyle); + buf.append(createDeclaration("a:link", linkDeclarationBlock)); + } + defaultLinkStyle = (StyleWithProperties) getStyles().getStyleByDisplayName(DEFAULT_VISITED_LINK_STYLE); + if (defaultLinkStyle != null) { + String visitedLinkDeclarationBlock = composeVisitedLinkDeclarationBlock(defaultLinkStyle); + buf.append(createDeclaration("a:visited", visitedLinkDeclarationBlock)); + } + + // Remaining link styles... + Enumeration enumer = anchorCombinedStyleNames.elements(); + while (enumer.hasMoreElements()) { + String exportName = enumer.nextElement(); + String styleName = orgAnchorStyleNames.get(exportName); + StyleWithProperties style = ofr.getTextStyle(styleName); + + if (style == null) { continue; } + + String anchorDeclarationBlock = composeAnchorDeclarationBlock(styleName); + String anchorSelector = composeAnchorSelector(exportName); + buf.append(createDeclaration(anchorSelector, anchorDeclarationBlock)); + + String visitedStyleName = orgAnchorVisitedStyleNames.get(exportName); + style = ofr.getTextStyle(visitedStyleName); + + String visitedAnchorDeclarationBlock = composeVisitedAnchorDeclarationBlock(visitedStyleName); + String visitedAnchorSelector = composeVisitedAnchorSelector(exportName); + buf.append(createDeclaration(visitedAnchorSelector, visitedAnchorDeclarationBlock)); + } + return buf.toString(); + } - // Remaining link styles... - Enumeration enumer = anchorCombinedStyleNames.elements(); - while (enumer.hasMoreElements()) { - String sExportName = enumer.nextElement(); - String sStyleName = orgAnchorStyleNames.get(sExportName); - String sVisitedStyleName = orgAnchorVisitedStyleNames.get(sExportName); + private String composeVisitedAnchorSelector(String exportName) { + String visitedAnchorSelector = "a." + exportName + ":visited"; + return visitedAnchorSelector; + } - StyleWithProperties style = ofr.getTextStyle(sStyleName); + private String composeVisitedAnchorDeclarationBlock(String visitedStyleName) { + StyleWithProperties style; + style = ofr.getTextStyle(visitedStyleName); + CSVList props = new CSVList(";"); + cssText(style, props, true); + cssHyperlink(style, props); + String visitedAnchorDeclarationBlock = props.toString(); + return visitedAnchorDeclarationBlock; + } - if (style!=null) { - CSVList props = new CSVList(";"); - cssText(style,props,true); - cssHyperlink(style,props); - buf.append(indent); - buf.append("a."); - buf.append(sExportName); - buf.append(":link {"); - buf.append(props.toString()); - buf.append("}\n"); - } - - style = ofr.getTextStyle(sVisitedStyleName); - if (style!=null) { - CSVList props = new CSVList(";"); - cssText(style,props,true); - cssHyperlink(style,props); - buf.append(indent); - buf.append("a."); - buf.append(sExportName); - buf.append(":visited {"); - buf.append(props.toString()); - buf.append("}\n"); - } - } - } - return buf.toString(); + private String composeAnchorSelector(String sExportName) { + String anchorSelector = "a." + sExportName + ":link"; + return anchorSelector; + } - } + private String composeAnchorDeclarationBlock(String sStyleName) { + StyleWithProperties style = ofr.getTextStyle(sStyleName); + CSVList props = new CSVList(";"); + cssText(style, props, true); + cssHyperlink(style, props); + String anchorDeclaration = props.toString(); + return anchorDeclaration; + } + + private String composeVisitedLinkDeclarationBlock(StyleWithProperties defaultLinkStyle) { + CSVList props = new CSVList(";"); + cssText(defaultLinkStyle, props, true); + cssHyperlink(defaultLinkStyle, props); + String visitedLinkDeclaration = props.toString(); + return visitedLinkDeclaration; + } + + private String composeLinkDeclarationBlock(StyleWithProperties defaultLinkStyle) { + CSVList props = new CSVList(";"); + cssText(defaultLinkStyle, props, true); + cssHyperlink(defaultLinkStyle, props); + String linkDeclaration = props.toString(); + return linkDeclaration; + } /** Get the family of text (character) styles * @return the style family