fb2html.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "fb2html.h"
  2. #include "fb2utils.h"
  3. #include "fb2text.hpp"
  4. //---------------------------------------------------------------------------
  5. // FbTextElement::Scheme
  6. //---------------------------------------------------------------------------
  7. FbTextElement::Scheme::Scheme()
  8. {
  9. m_types["body"]
  10. << Type("image")
  11. << Type("title")
  12. << Type("epigraph", 0, 0)
  13. << Type()
  14. ;
  15. m_types["section"]
  16. << Type("title")
  17. << Type("epigraph", 0, 0)
  18. << Type("image")
  19. << Type("annotation")
  20. << Type()
  21. ;
  22. m_types["poem"]
  23. << Type("title")
  24. << Type("epigraph", 0, 0)
  25. << Type("stanza", 1, 0)
  26. << Type()
  27. << Type("text-author", 0, 0)
  28. << Type("date")
  29. ;
  30. m_types["stanza"]
  31. << Type("title")
  32. << Type("subtitle")
  33. << Type()
  34. ;
  35. m_types["epigraph"]
  36. << Type()
  37. << Type("text-author", 0, 0)
  38. ;
  39. m_types["cite"]
  40. << Type()
  41. << Type("text-author", 0, 0)
  42. ;
  43. }
  44. const FbTextElement::TypeList * FbTextElement::Scheme::operator[](const QString &name) const
  45. {
  46. TypeMap::const_iterator it = m_types.find(name);
  47. if (it != m_types.end()) return &it.value();
  48. return 0;
  49. }
  50. //---------------------------------------------------------------------------
  51. // FbTextElement::Sublist
  52. //---------------------------------------------------------------------------
  53. FbTextElement::Sublist::Sublist(const TypeList &list, const QString &name)
  54. : m_list(list)
  55. , m_pos(list.begin())
  56. {
  57. TypeList::const_iterator empty = list.end();
  58. while (m_pos != list.end()) {
  59. if (m_pos->name() == name) break;
  60. if (m_pos->name().isEmpty()) empty = m_pos;
  61. m_pos++;
  62. }
  63. if (m_pos == list.end()) m_pos = empty;
  64. }
  65. FbTextElement::Sublist::operator bool() const
  66. {
  67. return m_pos != m_list.end();
  68. }
  69. bool FbTextElement::Sublist::operator!() const
  70. {
  71. return m_pos == m_list.end();
  72. }
  73. bool FbTextElement::Sublist::operator <(const QWebElement &element) const
  74. {
  75. const QString name = element.attribute("class");
  76. for (TypeList::const_iterator it = m_list.begin(); it != m_list.end(); it++) {
  77. if (it->name() == name) return m_pos < it || element.isNull();
  78. }
  79. return false;
  80. }
  81. bool FbTextElement::Sublist::operator >=(const QWebElement &element) const
  82. {
  83. const QString name = element.attribute("class");
  84. for (TypeList::const_iterator it = m_list.begin(); it != m_list.end(); it++) {
  85. if (it->name() == name) return m_pos >= it || element.isNull();
  86. }
  87. return false;
  88. }
  89. bool FbTextElement::Sublist::operator !=(const QWebElement &element) const
  90. {
  91. return element.isNull() || m_pos->name() != element.attribute("class");
  92. }
  93. //---------------------------------------------------------------------------
  94. // FbTextElement
  95. //---------------------------------------------------------------------------
  96. void FbTextElement::getChildren(FbElementList &list)
  97. {
  98. FbTextElement child = firstChild();
  99. while (!child.isNull()) {
  100. QString tag = child.tagName().toLower();
  101. if (tag == "div") {
  102. if (child.hasAttribute("class")) list << child;
  103. } else if (tag == "img") {
  104. list << child;
  105. } else {
  106. child.getChildren(list);
  107. }
  108. child = child.nextSibling();
  109. }
  110. }
  111. bool FbTextElement::hasScheme() const
  112. {
  113. return subtypes();
  114. }
  115. const FbTextElement::TypeList * FbTextElement::subtypes() const
  116. {
  117. static Scheme scheme;
  118. return scheme[attribute("class").toLower()];
  119. }
  120. bool FbTextElement::hasSubtype(const QString &style) const
  121. {
  122. if (const TypeList * list = subtypes()) {
  123. for (TypeList::const_iterator item = list->begin(); item != list->end(); item++) {
  124. if (item->name() == style) return true;
  125. }
  126. }
  127. return false;
  128. }
  129. FbTextElement::TypeList::const_iterator FbTextElement::subtype(const TypeList &list, const QString &style)
  130. {
  131. for (TypeList::const_iterator item = list.begin(); item != list.end(); item++) {
  132. if (item->name() == style) return item;
  133. }
  134. return list.end();
  135. }
  136. FbTextElement FbTextElement::insertInside(const QString &style, const QString &html)
  137. {
  138. const TypeList * types = subtypes();
  139. if (!types) return FbTextElement();
  140. Sublist sublist(*types, style);
  141. if (!sublist) return FbTextElement();
  142. FbTextElement child = firstChild();
  143. if (sublist < child) {
  144. prependInside(html);
  145. return firstChild();
  146. }
  147. while (!child.isNull()) {
  148. FbTextElement subling = child.nextSibling();
  149. if (sublist >= child && sublist != subling) {
  150. child.appendOutside(html);
  151. return child.nextSibling();
  152. }
  153. child = subling;
  154. }
  155. return FbTextElement();
  156. }
  157. QString FbTextElement::location()
  158. {
  159. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=this;");
  160. return evaluateJavaScript(javascript).toString();
  161. }
  162. void FbTextElement::select()
  163. {
  164. static const QString javascript = FB2::read(":/js/set_cursor.js");
  165. evaluateJavaScript(javascript);
  166. }
  167. bool FbTextElement::hasChild(const QString &style) const
  168. {
  169. FbTextElement child = firstChild();
  170. while (!child.isNull()) {
  171. if (child.tagName() == "DIV" && child.attribute("class").toLower() == style) return true;
  172. child = child.nextSibling();
  173. }
  174. return false;
  175. }
  176. bool FbTextElement::isDiv(const QString &style) const
  177. {
  178. return tagName() == "DIV" && attribute("class").toLower() == style;
  179. }
  180. bool FbTextElement::isBody() const
  181. {
  182. return isDiv("body");
  183. }
  184. bool FbTextElement::isSection() const
  185. {
  186. return isDiv("section");
  187. }
  188. bool FbTextElement::isTitle() const
  189. {
  190. return isDiv("title");
  191. }
  192. bool FbTextElement::isStanza() const
  193. {
  194. return isDiv("stanza");
  195. }
  196. bool FbTextElement::hasTitle() const
  197. {
  198. return FbTextElement(firstChild()).isTitle();
  199. }
  200. //---------------------------------------------------------------------------
  201. // FbInsertCmd
  202. //---------------------------------------------------------------------------
  203. FbInsertCmd::FbInsertCmd(const FbTextElement &element)
  204. : QUndoCommand()
  205. , m_element(element)
  206. , m_parent(element.previousSibling())
  207. , m_inner(false)
  208. {
  209. if (m_parent.isNull()) {
  210. m_parent = m_element.parent();
  211. m_inner = true;
  212. }
  213. }
  214. void FbInsertCmd::redo()
  215. {
  216. if (m_inner) {
  217. m_parent.prependInside(m_element);
  218. } else {
  219. m_parent.appendOutside(m_element);
  220. }
  221. m_element.select();
  222. }
  223. void FbInsertCmd::undo()
  224. {
  225. m_element.takeFromDocument();
  226. }
  227. //---------------------------------------------------------------------------
  228. // FbDeleteCmd
  229. //---------------------------------------------------------------------------
  230. FbDeleteCmd::FbDeleteCmd(const FbTextElement &element)
  231. : QUndoCommand()
  232. , m_element(element)
  233. , m_parent(element.previousSibling())
  234. , m_inner(false)
  235. {
  236. if (m_parent.isNull()) {
  237. m_parent = element.parent();
  238. m_inner = true;
  239. }
  240. }
  241. void FbDeleteCmd::redo()
  242. {
  243. m_element.takeFromDocument();
  244. }
  245. void FbDeleteCmd::undo()
  246. {
  247. if (m_inner) {
  248. m_parent.prependInside(m_element);
  249. } else {
  250. m_parent.appendOutside(m_element);
  251. }
  252. m_element.select();
  253. }
  254. //---------------------------------------------------------------------------
  255. // FbMoveUpCmd
  256. //---------------------------------------------------------------------------
  257. FbMoveUpCmd::FbMoveUpCmd(const FbTextElement &element)
  258. : QUndoCommand()
  259. , m_element(element)
  260. {
  261. }
  262. void FbMoveUpCmd::redo()
  263. {
  264. FbTextElement subling = m_element.previousSibling();
  265. subling.prependOutside(m_element.takeFromDocument());
  266. }
  267. void FbMoveUpCmd::undo()
  268. {
  269. FbTextElement subling = m_element.nextSibling();
  270. subling.appendOutside(m_element.takeFromDocument());
  271. }
  272. //---------------------------------------------------------------------------
  273. // FbMoveLeftCmd
  274. //---------------------------------------------------------------------------
  275. FbMoveLeftCmd::FbMoveLeftCmd(const FbTextElement &element)
  276. : QUndoCommand()
  277. , m_element(element)
  278. , m_subling(element.previousSibling())
  279. , m_parent(element.parent())
  280. {
  281. }
  282. void FbMoveLeftCmd::redo()
  283. {
  284. m_parent.appendOutside(m_element.takeFromDocument());
  285. }
  286. void FbMoveLeftCmd::undo()
  287. {
  288. if (m_subling.isNull()) {
  289. m_parent.prependInside(m_element.takeFromDocument());
  290. } else {
  291. m_subling.appendOutside(m_element.takeFromDocument());
  292. }
  293. }
  294. //---------------------------------------------------------------------------
  295. // FbMoveRightCmd
  296. //---------------------------------------------------------------------------
  297. FbMoveRightCmd::FbMoveRightCmd(const FbTextElement &element)
  298. : QUndoCommand()
  299. , m_element(element)
  300. , m_subling(element.previousSibling())
  301. {
  302. }
  303. void FbMoveRightCmd::redo()
  304. {
  305. m_subling.appendInside(m_element.takeFromDocument());
  306. }
  307. void FbMoveRightCmd::undo()
  308. {
  309. m_subling.appendOutside(m_element.takeFromDocument());
  310. }