fb2html.cpp 8.3 KB

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