Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theirix/d7a9472db48bcc81a6b8 to your computer and use it in GitHub Desktop.
Save theirix/d7a9472db48bcc81a6b8 to your computer and use it in GitHub Desktop.
From a4e8b94c553940d1a741895a3e6a54deb1f03085 Mon Sep 17 00:00:00 2001
From: theirix <theirix@gmail.com>
Date: Sun, 3 May 2015 19:05:41 +0300
Subject: [PATCH] Autodetect correct TidyBodyOnly parameter type
Autodetect which version (Int or Bool) should be used.
TidyBodyOnly has different types in different versions
(tidy 20061031 on OS X and 20091223 on debian).
---
src/util.cpp | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/src/util.cpp b/src/util.cpp
index b9e8bf1..8e0b6ad 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -277,6 +277,24 @@ QString removeHtml(QString origText) {
//------------------------------------------------------------------------------
+#ifndef NO_TIDY
+// Tidy changed TidyBodyOnly type from bool to int at 2007-05-24.
+// Returns true when a new version (int) is found
+bool isTidyWithIntBodyOnly()
+{
+ QString releaseDateStr(tidyReleaseDate());
+ int yearPos = releaseDateStr.indexOf(QRegExp(" [0-9]{4}"));
+ if (yearPos != -1)
+ releaseDateStr = releaseDateStr.left(yearPos + 5);
+ QDate releaseDate = QLocale::c().toDate(releaseDateStr, "d MMMM yyyy");
+ QDate changeDate = QLocale::c().toDate("24 May 2007", "d MMMM yyyy");
+ bool isNewer = releaseDate > changeDate;
+ qDebug() << "\n[DEBUG] tidy release date:" << releaseDate.toString(Qt::ISODate);
+ qDebug() << "\n[DEBUG] use API with new TidyBodyOnly as int :" << isNewer;
+ return isNewer;
+}
+#endif
+
QString tidyHtml(QString str, bool& ok) {
#ifdef NO_TIDY
ok = true;
@@ -296,7 +314,9 @@ QString tidyHtml(QString str, bool& ok) {
tidyOptSetBool(tdoc, TidyXhtmlOut, yes) &&
tidyOptSetBool(tdoc, TidyForceOutput, yes) &&
tidyOptSetBool(tdoc, TidyMark, no) &&
- tidyOptSetInt(tdoc, TidyBodyOnly, yes) &&
+ (isTidyWithIntBodyOnly()
+ ? tidyOptSetInt(tdoc, TidyBodyOnly, 1)
+ : tidyOptSetBool(tdoc, TidyBodyOnly, yes)) &&
tidyOptSetInt(tdoc, TidyWrapLen, 0) &&
tidyOptSetInt(tdoc, TidyDoctypeMode, TidyDoctypeOmit);
--
2.3.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment