fb2html.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 FbTextElement &element) const
  74. {
  75. const QString name = element.nodeName();
  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 FbTextElement &element) const
  82. {
  83. const QString name = element.nodeName();
  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 FbTextElement &element) const
  90. {
  91. return element.isNull() || m_pos->name() != element.nodeName();
  92. }
  93. //---------------------------------------------------------------------------
  94. // FbTextElement
  95. //---------------------------------------------------------------------------
  96. QString FbTextElement::blockName() const
  97. {
  98. QString n = tagName().toLower();
  99. return n.left(3) == "fb:" ? n.mid(3) : QString();
  100. }
  101. QString FbTextElement::nodeName() const
  102. {
  103. QString n = tagName().toLower();
  104. return n.left(3) == "fb:" ? n.mid(3) : n;
  105. }
  106. void FbTextElement::getChildren(FbElementList &list)
  107. {
  108. FbTextElement child = firstChild();
  109. while (!child.isNull()) {
  110. QString tag = child.tagName().toLower();
  111. if (tag == "fb:description") {
  112. // skip description
  113. } else if (tag.left(3) == "fb:") {
  114. list << child;
  115. } else if (tag == "img") {
  116. list << child;
  117. } else {
  118. child.getChildren(list);
  119. }
  120. child = child.nextSibling();
  121. }
  122. }
  123. bool FbTextElement::hasScheme() const
  124. {
  125. return subtypes();
  126. }
  127. const FbTextElement::TypeList * FbTextElement::subtypes() const
  128. {
  129. static Scheme scheme;
  130. return scheme[nodeName()];
  131. }
  132. bool FbTextElement::hasSubtype(const QString &style) const
  133. {
  134. if (const TypeList * list = subtypes()) {
  135. for (TypeList::const_iterator item = list->begin(); item != list->end(); item++) {
  136. if (item->name() == style) return true;
  137. }
  138. }
  139. return false;
  140. }
  141. FbTextElement::TypeList::const_iterator FbTextElement::subtype(const TypeList &list, const QString &style)
  142. {
  143. for (TypeList::const_iterator item = list.begin(); item != list.end(); item++) {
  144. if (item->name() == style) return item;
  145. }
  146. return list.end();
  147. }
  148. FbTextElement FbTextElement::insertInside(const QString &style, const QString &html)
  149. {
  150. const TypeList * types = subtypes();
  151. if (!types) return FbTextElement();
  152. Sublist sublist(*types, style);
  153. if (!sublist) return FbTextElement();
  154. FbTextElement child = firstChild();
  155. if (sublist < child) {
  156. prependInside(html);
  157. return firstChild();
  158. }
  159. while (!child.isNull()) {
  160. FbTextElement subling = child.nextSibling();
  161. if (sublist >= child && sublist != subling) {
  162. child.appendOutside(html);
  163. return child.nextSibling();
  164. }
  165. child = subling;
  166. }
  167. return FbTextElement();
  168. }
  169. QString FbTextElement::location()
  170. {
  171. return evaluateJavaScript("location(this)").toString();
  172. }
  173. void FbTextElement::select()
  174. {
  175. QString javascript = jScript("set_cursor.js");
  176. evaluateJavaScript(javascript);
  177. }
  178. bool FbTextElement::hasChild(const QString &style) const
  179. {
  180. FbTextElement child = firstChild();
  181. while (!child.isNull()) {
  182. if (child.nodeName() == style) return true;
  183. child = child.nextSibling();
  184. }
  185. return false;
  186. }
  187. bool FbTextElement::isBody() const
  188. {
  189. return nodeName() == "body";
  190. }
  191. bool FbTextElement::isSection() const
  192. {
  193. return nodeName() == "section";
  194. }
  195. bool FbTextElement::isTitle() const
  196. {
  197. return nodeName() == "title";
  198. }
  199. bool FbTextElement::isStanza() const
  200. {
  201. return nodeName() == "stanza";
  202. }
  203. bool FbTextElement::hasTitle() const
  204. {
  205. return FbTextElement(firstChild()).isTitle();
  206. }
  207. int FbTextElement::index() const
  208. {
  209. int result = -1;
  210. FbTextElement prior = *this;
  211. while (!prior.isNull()) {
  212. prior = prior.previousSibling();
  213. result++;
  214. }
  215. return result;
  216. }
  217. FbTextElement FbTextElement::child(int index) const
  218. {
  219. FbTextElement result = firstChild();
  220. while (index > 0) {
  221. result = result.nextSibling();
  222. index--;
  223. }
  224. return index ? FbTextElement() : result;
  225. }
  226. //---------------------------------------------------------------------------
  227. // FbInsertCmd
  228. //---------------------------------------------------------------------------
  229. FbInsertCmd::FbInsertCmd(const FbTextElement &element)
  230. : QUndoCommand()
  231. , m_element(element)
  232. , m_parent(element.previousSibling())
  233. , m_inner(false)
  234. {
  235. if (m_parent.isNull()) {
  236. m_parent = m_element.parent();
  237. m_inner = true;
  238. }
  239. }
  240. void FbInsertCmd::redo()
  241. {
  242. if (m_inner) {
  243. m_parent.prependInside(m_element);
  244. } else {
  245. m_parent.appendOutside(m_element);
  246. }
  247. m_element.select();
  248. }
  249. void FbInsertCmd::undo()
  250. {
  251. m_element.takeFromDocument();
  252. }
  253. //---------------------------------------------------------------------------
  254. // FbReplaceCmd
  255. //---------------------------------------------------------------------------
  256. FbReplaceCmd::FbReplaceCmd(const FbTextElement &original, const FbTextElement &duplicate)
  257. : QUndoCommand()
  258. , m_original(original)
  259. , m_duplicate(duplicate)
  260. , m_update(false)
  261. {
  262. }
  263. void FbReplaceCmd::redo()
  264. {
  265. if (m_update) {
  266. m_original.prependOutside(m_duplicate);
  267. m_original.takeFromDocument();
  268. m_duplicate.select();
  269. } else {
  270. m_update = true;
  271. }
  272. }
  273. void FbReplaceCmd::undo()
  274. {
  275. m_duplicate.prependOutside(m_original);
  276. m_duplicate.takeFromDocument();
  277. m_original.select();
  278. }
  279. //---------------------------------------------------------------------------
  280. // FbDeleteCmd
  281. //---------------------------------------------------------------------------
  282. FbDeleteCmd::FbDeleteCmd(const FbTextElement &element)
  283. : QUndoCommand()
  284. , m_element(element)
  285. , m_parent(element.previousSibling())
  286. , m_inner(false)
  287. {
  288. if (m_parent.isNull()) {
  289. m_parent = element.parent();
  290. m_inner = true;
  291. }
  292. }
  293. void FbDeleteCmd::redo()
  294. {
  295. m_element.takeFromDocument();
  296. }
  297. void FbDeleteCmd::undo()
  298. {
  299. if (m_inner) {
  300. m_parent.prependInside(m_element);
  301. } else {
  302. m_parent.appendOutside(m_element);
  303. }
  304. m_element.select();
  305. }
  306. //---------------------------------------------------------------------------
  307. // FbMoveUpCmd
  308. //---------------------------------------------------------------------------
  309. FbMoveUpCmd::FbMoveUpCmd(const FbTextElement &element)
  310. : QUndoCommand()
  311. , m_element(element)
  312. {
  313. }
  314. void FbMoveUpCmd::redo()
  315. {
  316. FbTextElement subling = m_element.previousSibling();
  317. subling.prependOutside(m_element.takeFromDocument());
  318. }
  319. void FbMoveUpCmd::undo()
  320. {
  321. FbTextElement subling = m_element.nextSibling();
  322. subling.appendOutside(m_element.takeFromDocument());
  323. }
  324. //---------------------------------------------------------------------------
  325. // FbMoveLeftCmd
  326. //---------------------------------------------------------------------------
  327. FbMoveLeftCmd::FbMoveLeftCmd(const FbTextElement &element)
  328. : QUndoCommand()
  329. , m_element(element)
  330. , m_subling(element.previousSibling())
  331. , m_parent(element.parent())
  332. {
  333. }
  334. void FbMoveLeftCmd::redo()
  335. {
  336. m_parent.appendOutside(m_element.takeFromDocument());
  337. }
  338. void FbMoveLeftCmd::undo()
  339. {
  340. if (m_subling.isNull()) {
  341. m_parent.prependInside(m_element.takeFromDocument());
  342. } else {
  343. m_subling.appendOutside(m_element.takeFromDocument());
  344. }
  345. }
  346. //---------------------------------------------------------------------------
  347. // FbMoveRightCmd
  348. //---------------------------------------------------------------------------
  349. FbMoveRightCmd::FbMoveRightCmd(const FbTextElement &element)
  350. : QUndoCommand()
  351. , m_element(element)
  352. , m_subling(element.previousSibling())
  353. {
  354. }
  355. void FbMoveRightCmd::redo()
  356. {
  357. m_subling.appendInside(m_element.takeFromDocument());
  358. }
  359. void FbMoveRightCmd::undo()
  360. {
  361. m_subling.appendOutside(m_element.takeFromDocument());
  362. }