fb2html.cpp 8.4 KB

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