From e8631bf7ed277b4eaf6bebc7d9d170e558be6226 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Tue, 27 Oct 2009 14:33:01 +0000 Subject: [PATCH] Default CSS for InsetLayout. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31771 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetLayout.cpp | 69 ++++++++++++++++++++++++++++++++++++++ src/insets/InsetLayout.h | 28 +++++++++++++--- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/insets/InsetLayout.cpp b/src/insets/InsetLayout.cpp index 9534aeb3a7..fd1067d767 100644 --- a/src/insets/InsetLayout.cpp +++ b/src/insets/InsetLayout.cpp @@ -35,6 +35,7 @@ InsetLayout::InsetLayout() : labelstring_(from_ascii("UNDEFINED")), decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font), labelfont_(sane_font), bgcolor_(Color_error), + htmlforcecss_ (false), htmlisblock_(true), multipar_(false), custompars_(true), forceplain_(false), passthru_(false), needprotect_(false), freespacing_(false), keepempty_(false), forceltr_(false), intoc_(false) @@ -84,6 +85,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass) IL_FREESPACING, IL_HTMLTAG, IL_HTMLATTR, + IL_HTMLFORCECSS, IL_HTMLINNERTAG, IL_HTMLINNERATTR, IL_HTMLISBLOCK, @@ -119,6 +121,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass) { "forceplain", IL_FORCEPLAIN }, { "freespacing", IL_FREESPACING }, { "htmlattr", IL_HTMLATTR }, + { "htmlforcecss", IL_HTMLFORCECSS }, { "htmlinnerattr", IL_HTMLINNERATTR}, { "htmlinnertag", IL_HTMLINNERTAG}, { "htmlisblock", IL_HTMLISBLOCK}, @@ -285,6 +288,9 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass) case IL_HTMLATTR: lex >> htmlattr_; break; + case IL_HTMLFORCECSS: + lex >> htmlforcecss_; + break; case IL_HTMLINNERTAG: lex >> htmlinnertag_; break; @@ -345,4 +351,67 @@ InsetLayout::InsetLyXType translateLyXType(std::string const & str) return InsetLayout::NOLYXTYPE; } + +string InsetLayout::defaultCSSClass() const +{ + if (!defaultcssclass_.empty()) + return defaultcssclass_; + docstring d; + docstring::const_iterator it = name().begin(); + docstring::const_iterator en = name().end(); + for (; it != en; ++it) { + if (!isalpha(*it)) + continue; + if (islower(*it)) + d += *it; + else + d += support::lowercase(*it); + } + // are there other characters we need to remove? + defaultcssclass_ = to_utf8(d); + return defaultcssclass_; +} + + +void InsetLayout::makeDefaultCSS() const +{ +#ifdef TEX2LYX + // tex2lyx does not have FontInfo::asCSS() + return; +#else + if (!htmldefaultstyle_.empty()) + return; + if (!htmltag_.empty()) { + docstring const mainfontCSS = font_.asCSS(); + if (!mainfontCSS.empty()) + htmldefaultstyle_ = + from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") + + mainfontCSS + from_ascii("\n}\n"); + } + /* + At present, we do not have default tags, etc, for the label. + if (labelfont_ == font_) + return; + docstring const labelfontCSS = labelfont_.asCSS(); + if (!labelfontCSS.empty()) + htmldefaultstyle_ += +. from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") + + labelfontCSS + from_ascii("\n}\n"); + */ +#endif +} + +docstring InsetLayout::htmlstyle() const +{ + if (!htmlstyle_.empty() && !htmlforcecss_) + return htmlstyle_; + if (htmldefaultstyle_.empty()) + makeDefaultCSS(); + docstring retval = htmldefaultstyle_; + if (!htmlstyle_.empty()) + retval += '\n' + htmlstyle_; + return retval; +} + + } //namespace lyx diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h index ccc1c46307..6ebb916082 100644 --- a/src/insets/InsetLayout.h +++ b/src/insets/InsetLayout.h @@ -93,7 +93,7 @@ public: /// std::string const & htmllabel() const { return htmllabel_; } /// - docstring htmlstyle() const { return htmlstyle_; } + docstring htmlstyle() const; /// docstring htmlpreamble() const { return htmlpreamble_; } /// @@ -118,7 +118,14 @@ public: bool forceLTR() const { return forceltr_; }; /// bool isInToc() const { return intoc_; }; + /// private: + /// + void makeDefaultCSS() const; + /// + std::string defaultCSSClass() const; + /// + std::string defaultCSSLabelClass() const { return defaultCSSClass() + "_label"; } /// docstring name_; /** @@ -147,20 +154,31 @@ private: docstring counter_; /// docstring preamble_; - /// The tag enclosing all the material in this inset. + /// The tag enclosing all the material in this inset. Default is none. std::string htmltag_; - /// Additional attributes for inclusion with the start tag. + /// Additional attributes for inclusion with the start tag. Default (if + /// a tag is provided) is: class="name". std::string htmlattr_; - /// Tag for individual paragraphs in the inset. + /// Tag for individual paragraphs in the inset. Default is none. std::string htmlinnertag_; - /// Attributes for that tag. + /// Attributes for that tag. Default (if a tag is provided) is: + /// class="name_inner". std::string htmlinnerattr_; /// A label for this environment, possibly including a reference /// to a counter. E.g., for footnote, it might be: /// \arabic{footnote} + /// No default. + /// FIXME Could we get this from the layout? std::string htmllabel_; /// CSS associated with this inset. docstring htmlstyle_; + /// Cache for default CSS info for this inset. + mutable docstring htmldefaultstyle_; + /// + mutable std::string defaultcssclass_; + /// Whether to force generation of default CSS even if some is given. + /// False by default. + bool htmlforcecss_; /// Additional material for the header. docstring htmlpreamble_; /// Whether this inset represents a "block" of material, i.e., a set -- 2.39.2