fb2html.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. while (m_pos != list.end()) {
  58. if (m_pos->name() == name) break;
  59. m_pos++;
  60. }
  61. }
  62. FbTextElement::Sublist::operator bool() const
  63. {
  64. return m_pos != m_list.end();
  65. }
  66. bool FbTextElement::Sublist::operator!() const
  67. {
  68. return m_pos == m_list.end();
  69. }
  70. bool FbTextElement::Sublist::operator <(const QWebElement &element) const
  71. {
  72. const QString name = element.attribute("class");
  73. for (TypeList::const_iterator it = m_list.begin(); it != m_list.end(); it++) {
  74. if (it->name() == name) return it < m_pos || element.isNull();
  75. }
  76. return false;
  77. }
  78. //---------------------------------------------------------------------------
  79. // FbTextElement
  80. //---------------------------------------------------------------------------
  81. void FbTextElement::getChildren(FbElementList &list)
  82. {
  83. FbTextElement child = firstChild();
  84. while (!child.isNull()) {
  85. QString tag = child.tagName().toLower();
  86. if (tag == "div") {
  87. if (child.hasAttribute("class")) list << child;
  88. } else if (tag == "img") {
  89. list << child;
  90. } else {
  91. child.getChildren(list);
  92. }
  93. child = child.nextSibling();
  94. }
  95. }
  96. const FbTextElement::TypeList * FbTextElement::subtypes() const
  97. {
  98. static Scheme scheme;
  99. return scheme[tagName().toLower()];
  100. }
  101. FbTextElement::TypeList::const_iterator FbTextElement::subtype(const TypeList &list, const QString &style)
  102. {
  103. for (TypeList::const_iterator item = list.begin(); item != list.end(); item++) {
  104. if (item->name() == style) return item;
  105. }
  106. return list.end();
  107. }
  108. FbTextElement FbTextElement::insertInside(const QString &style, const QString &html)
  109. {
  110. const TypeList * types = subtypes();
  111. if (!types) return FbTextElement();
  112. Sublist sublist(*types, style);
  113. if (!sublist) return FbTextElement();
  114. FbTextElement child = firstChild();
  115. if (sublist < child) {
  116. prependInside(html);
  117. return firstChild();
  118. }
  119. }
  120. QString FbTextElement::location()
  121. {
  122. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=this;");
  123. return evaluateJavaScript(javascript).toString();
  124. }
  125. void FbTextElement::select()
  126. {
  127. static const QString javascript = FB2::read(":/js/set_cursor.js");
  128. evaluateJavaScript(javascript);
  129. }
  130. bool FbTextElement::hasChild(const QString &style) const
  131. {
  132. FbTextElement child = firstChild();
  133. while (!child.isNull()) {
  134. if (child.tagName() == "DIV" && child.attribute("class").toLower() == style) return true;
  135. child = child.nextSibling();
  136. }
  137. return false;
  138. }
  139. bool FbTextElement::isDiv(const QString &style) const
  140. {
  141. return tagName() == "DIV" && attribute("class").toLower() == style;
  142. }
  143. bool FbTextElement::isBody() const
  144. {
  145. return isDiv("body");
  146. }
  147. bool FbTextElement::isSection() const
  148. {
  149. return isDiv("section");
  150. }
  151. bool FbTextElement::isTitle() const
  152. {
  153. return isDiv("title");
  154. }
  155. bool FbTextElement::isStanza() const
  156. {
  157. return isDiv("stanza");
  158. }
  159. bool FbTextElement::hasTitle() const
  160. {
  161. return FbTextElement(firstChild()).isTitle();
  162. }
  163. //---------------------------------------------------------------------------
  164. // FbInsertCmd
  165. //---------------------------------------------------------------------------
  166. FbInsertCmd::FbInsertCmd(const FbTextElement &element)
  167. : QUndoCommand()
  168. , m_element(element)
  169. , m_parent(element.previousSibling())
  170. , m_inner(false)
  171. {
  172. if (m_parent.isNull()) {
  173. m_parent = m_element.parent();
  174. m_inner = true;
  175. }
  176. }
  177. void FbInsertCmd::redo()
  178. {
  179. if (m_inner) {
  180. m_parent.prependInside(m_element);
  181. } else {
  182. m_parent.appendOutside(m_element);
  183. }
  184. m_element.select();
  185. }
  186. void FbInsertCmd::undo()
  187. {
  188. m_element.takeFromDocument();
  189. }
  190. //---------------------------------------------------------------------------
  191. // FbDeleteCmd
  192. //---------------------------------------------------------------------------
  193. FbDeleteCmd::FbDeleteCmd(const FbTextElement &element)
  194. : QUndoCommand()
  195. , m_element(element)
  196. , m_parent(element.previousSibling())
  197. , m_inner(false)
  198. {
  199. if (m_parent.isNull()) {
  200. m_parent = element.parent();
  201. m_inner = true;
  202. }
  203. }
  204. void FbDeleteCmd::redo()
  205. {
  206. m_element.takeFromDocument();
  207. }
  208. void FbDeleteCmd::undo()
  209. {
  210. if (m_inner) {
  211. m_parent.prependInside(m_element);
  212. } else {
  213. m_parent.appendOutside(m_element);
  214. }
  215. m_element.select();
  216. }
  217. //---------------------------------------------------------------------------
  218. // FbMoveUpCmd
  219. //---------------------------------------------------------------------------
  220. FbMoveUpCmd::FbMoveUpCmd(const FbTextElement &element)
  221. : QUndoCommand()
  222. , m_element(element)
  223. {
  224. }
  225. void FbMoveUpCmd::redo()
  226. {
  227. FbTextElement subling = m_element.previousSibling();
  228. subling.prependOutside(m_element.takeFromDocument());
  229. }
  230. void FbMoveUpCmd::undo()
  231. {
  232. FbTextElement subling = m_element.nextSibling();
  233. subling.appendOutside(m_element.takeFromDocument());
  234. }
  235. //---------------------------------------------------------------------------
  236. // FbMoveLeftCmd
  237. //---------------------------------------------------------------------------
  238. FbMoveLeftCmd::FbMoveLeftCmd(const FbTextElement &element)
  239. : QUndoCommand()
  240. , m_element(element)
  241. , m_subling(element.previousSibling())
  242. , m_parent(element.parent())
  243. {
  244. }
  245. void FbMoveLeftCmd::redo()
  246. {
  247. m_parent.appendOutside(m_element.takeFromDocument());
  248. }
  249. void FbMoveLeftCmd::undo()
  250. {
  251. if (m_subling.isNull()) {
  252. m_parent.prependInside(m_element.takeFromDocument());
  253. } else {
  254. m_subling.appendOutside(m_element.takeFromDocument());
  255. }
  256. }
  257. //---------------------------------------------------------------------------
  258. // FbMoveRightCmd
  259. //---------------------------------------------------------------------------
  260. FbMoveRightCmd::FbMoveRightCmd(const FbTextElement &element)
  261. : QUndoCommand()
  262. , m_element(element)
  263. , m_subling(element.previousSibling())
  264. {
  265. }
  266. void FbMoveRightCmd::redo()
  267. {
  268. m_subling.appendInside(m_element.takeFromDocument());
  269. }
  270. void FbMoveRightCmd::undo()
  271. {
  272. m_subling.appendOutside(m_element.takeFromDocument());
  273. }