diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 6fd6fad5a..27bcf932f 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -601,6 +601,30 @@ function ReaderHighlight:lookup(selected_word, selected_link) end end +local function prettifyCss(css_text) + -- This is not perfect, but enough to make some ugly CSS readable. + -- Get rid of \t so we can use it as a replacement/hiding char + css_text = css_text:gsub("\t", " ") + -- Wrap and indent declarations + css_text = css_text:gsub("%s*{%s*", " {\n ") + css_text = css_text:gsub(";%s*}%s*", ";\n}\n") + css_text = css_text:gsub(";%s*([^}])", ";\n %1") + css_text = css_text:gsub("%s*}%s*", "\n}\n") + -- Cleanup declarations + css_text = css_text:gsub("{[^}]*}", function(s) + s = s:gsub("%s*:%s*", ": ") + -- Temporarily hide/replace ',' in declaration so they + -- are not matched and made multi-lines by followup gsub + s = s:gsub("%s*,%s*", "\t") + return s + end) + -- Have each selector (separated by ',') on a new line + css_text = css_text:gsub("%s*,%s*", " ,\n") + -- Restore hidden ',' in declarations + css_text = css_text:gsub("\t", ", ") + return css_text +end + function ReaderHighlight:viewSelectionHTML(debug_view) if self.ui.document.info.has_pages then return @@ -621,6 +645,15 @@ function ReaderHighlight:viewSelectionHTML(debug_view) if debug_view then html = html:gsub("\xC2\xA0", "␣") -- no break space: open box html = html:gsub("\xC2\xAD", "⋅") -- soft hyphen: dot operator (smaller than middle dot ·) + -- Prettify inlined CSS (from
, put in an internal + --