fb2html.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. << Type("section")
  15. ;
  16. m_types["section"]
  17. << Type("title")
  18. << Type("epigraph", 0, 0)
  19. << Type("image")
  20. << Type("annotation")
  21. << Type()
  22. << Type("section")
  23. ;
  24. m_types["poem"]
  25. << Type("title")
  26. << Type("epigraph", 0, 0)
  27. << Type("stanza", 1, 0)
  28. << Type()
  29. << Type("text-author", 0, 0)
  30. << Type("date")
  31. ;
  32. m_types["stanza"]
  33. << Type("title")
  34. << Type("subtitle")
  35. << Type()
  36. ;
  37. m_types["epigraph"]
  38. << Type()
  39. << Type("text-author", 0, 0)
  40. ;
  41. m_types["cite"]
  42. << Type()
  43. << Type("text-author", 0, 0)
  44. ;
  45. }
  46. const FbTextElement::TypeList * FbTextElement::Scheme::operator[](const QString &name) const
  47. {
  48. TypeMap::const_iterator it = m_types.find(name);
  49. if (it != m_types.end()) return &it.value();
  50. return 0;
  51. }
  52. //---------------------------------------------------------------------------
  53. // FbTextElement::Sublist
  54. //---------------------------------------------------------------------------
  55. FbTextElement::Sublist::Sublist(const TypeList &list, const QString &name)
  56. : m_list(list)
  57. , m_pos(list.begin())
  58. {
  59. TypeList::const_iterator empty = list.end();
  60. while (m_pos != list.end()) {
  61. if (m_pos->name() == name) break;
  62. if (m_pos->name().isEmpty()) empty = m_pos;
  63. m_pos++;
  64. }
  65. if (m_pos == list.end()) m_pos = empty;
  66. }
  67. FbTextElement::Sublist::operator bool() const
  68. {
  69. return m_pos != m_list.end();
  70. }
  71. bool FbTextElement::Sublist::operator!() const
  72. {
  73. return m_pos == m_list.end();
  74. }
  75. bool FbTextElement::Sublist::operator <(const FbTextElement &element) const
  76. {
  77. const QString name = element.nodeName();
  78. for (TypeList::const_iterator it = m_list.begin(); it != m_list.end(); it++) {
  79. if (it->name() == name) return m_pos < it || element.isNull();
  80. }
  81. return false;
  82. }
  83. bool FbTextElement::Sublist::operator >=(const FbTextElement &element) const
  84. {
  85. const QString name = element.nodeName();
  86. for (TypeList::const_iterator it = m_list.begin(); it != m_list.end(); it++) {
  87. if (it->name() == name) return m_pos >= it || element.isNull();
  88. }
  89. return false;
  90. }
  91. bool FbTextElement::Sublist::operator !=(const FbTextElement &element) const
  92. {
  93. return element.isNull() || m_pos->name() != element.nodeName();
  94. }
  95. //---------------------------------------------------------------------------
  96. // FbTextElement
  97. //---------------------------------------------------------------------------
  98. QString FbTextElement::blockName() const
  99. {
  100. QString n = tagName().toLower();
  101. return n.left(3) == "fb:" ? n.mid(3) : QString();
  102. }
  103. QString FbTextElement::nodeName() const
  104. {
  105. QString n = tagName().toLower();
  106. return n.left(3) == "fb:" ? n.mid(3) : n;
  107. }
  108. void FbTextElement::getChildren(FbElementList &list)
  109. {
  110. FbTextElement child = firstChild();
  111. while (!child.isNull()) {
  112. QString tag = child.tagName().toLower();
  113. if (tag == "fb:description") {
  114. // skip description
  115. } else if (tag.left(3) == "fb:") {
  116. list << child;
  117. } else if (tag == "img") {
  118. list << child;
  119. } else {
  120. child.getChildren(list);
  121. }
  122. child = child.nextSibling();
  123. }
  124. }
  125. int FbTextElement::childIndex() const
  126. {
  127. FbElementList list;
  128. parent().getChildren(list);
  129. int result = 0;
  130. FbElementList::const_iterator it;
  131. for (it = list.constBegin(); it != list.constEnd(); ++it) {
  132. if (*it == *this) return result;
  133. result++;
  134. }
  135. return -1;
  136. }
  137. bool FbTextElement::hasScheme() const
  138. {
  139. return subtypes();
  140. }
  141. const FbTextElement::TypeList * FbTextElement::subtypes() const
  142. {
  143. static Scheme scheme;
  144. return scheme[nodeName()];
  145. }
  146. bool FbTextElement::hasSubtype(const QString &style) const
  147. {
  148. if (const TypeList * list = subtypes()) {
  149. for (TypeList::const_iterator item = list->begin(); item != list->end(); item++) {
  150. if (item->name() == style) return true;
  151. }
  152. }
  153. return false;
  154. }
  155. FbTextElement::TypeList::const_iterator FbTextElement::subtype(const TypeList &list, const QString &style)
  156. {
  157. for (TypeList::const_iterator item = list.begin(); item != list.end(); item++) {
  158. if (item->name() == style) return item;
  159. }
  160. return list.end();
  161. }
  162. FbTextElement FbTextElement::insertInside(const QString &style, const QString &html)
  163. {
  164. const TypeList * types = subtypes();
  165. if (!types) return FbTextElement();
  166. Sublist sublist(*types, style);
  167. if (!sublist) return FbTextElement();
  168. FbTextElement child = firstChild();
  169. if (sublist < child) {
  170. prependInside(html);
  171. return firstChild();
  172. }
  173. while (!child.isNull()) {
  174. FbTextElement subling = child.nextSibling();
  175. if (sublist >= child && sublist != subling) {
  176. child.appendOutside(html);
  177. return child.nextSibling();
  178. }
  179. child = subling;
  180. }
  181. return FbTextElement();
  182. }
  183. QString FbTextElement::location()
  184. {
  185. return evaluateJavaScript("location(this)").toString();
  186. }
  187. void FbTextElement::select()
  188. {
  189. QString javascript = jScript("set_cursor.js");
  190. evaluateJavaScript(javascript);
  191. }
  192. bool FbTextElement::hasChild(const QString &style) const
  193. {
  194. FbTextElement child = firstChild();
  195. while (!child.isNull()) {
  196. if (child.tagName() == style) return true;
  197. child = child.nextSibling();
  198. }
  199. return false;
  200. }
  201. bool FbTextElement::isBody() const
  202. {
  203. return tagName() == "FB:BODY";
  204. }
  205. bool FbTextElement::isSection() const
  206. {
  207. return tagName() == "FB:SECTION";
  208. }
  209. bool FbTextElement::isTitle() const
  210. {
  211. return tagName() == "FB:TITLE";
  212. }
  213. bool FbTextElement::isStanza() const
  214. {
  215. return tagName() == "FB:STANZA";
  216. }
  217. bool FbTextElement::hasTitle() const
  218. {
  219. return hasChild("FB:TITLE");
  220. }
  221. int FbTextElement::index() const
  222. {
  223. int result = -1;
  224. FbTextElement prior = *this;
  225. while (!prior.isNull()) {
  226. prior = prior.previousSibling();
  227. result++;
  228. }
  229. return result;
  230. }
  231. FbTextElement FbTextElement::child(int index) const
  232. {
  233. FbTextElement result = firstChild();
  234. while (index > 0) {
  235. result = result.nextSibling();
  236. index--;
  237. }
  238. return index ? FbTextElement() : result;
  239. }
  240. //---------------------------------------------------------------------------
  241. // FbInsertCmd
  242. //---------------------------------------------------------------------------
  243. FbInsertCmd::FbInsertCmd(const FbTextElement &element)
  244. : QUndoCommand()
  245. , m_element(element)
  246. , m_parent(element.previousSibling())
  247. , m_inner(false)
  248. {
  249. if (m_parent.isNull()) {
  250. m_parent = m_element.parent();
  251. m_inner = true;
  252. }
  253. }
  254. void FbInsertCmd::redo()
  255. {
  256. if (m_inner) {
  257. m_parent.prependInside(m_element);
  258. } else {
  259. m_parent.appendOutside(m_element);
  260. }
  261. m_element.select();
  262. }
  263. void FbInsertCmd::undo()
  264. {
  265. m_element.takeFromDocument();
  266. }
  267. //---------------------------------------------------------------------------
  268. // FbReplaceCmd
  269. //---------------------------------------------------------------------------
  270. FbReplaceCmd::FbReplaceCmd(const FbTextElement &original, const FbTextElement &duplicate)
  271. : QUndoCommand()
  272. , m_original(original)
  273. , m_duplicate(duplicate)
  274. , m_update(false)
  275. {
  276. }
  277. void FbReplaceCmd::redo()
  278. {
  279. if (m_update) {
  280. m_original.prependOutside(m_duplicate);
  281. m_original.takeFromDocument();
  282. m_duplicate.select();
  283. } else {
  284. m_update = true;
  285. }
  286. }
  287. void FbReplaceCmd::undo()
  288. {
  289. m_duplicate.prependOutside(m_original);
  290. m_duplicate.takeFromDocument();
  291. m_original.select();
  292. }
  293. //---------------------------------------------------------------------------
  294. // FbDeleteCmd
  295. //---------------------------------------------------------------------------
  296. FbDeleteCmd::FbDeleteCmd(const FbTextElement &element)
  297. : QUndoCommand()
  298. , m_element(element)
  299. , m_parent(element.previousSibling())
  300. , m_inner(false)
  301. {
  302. if (m_parent.isNull()) {
  303. m_parent = element.parent();
  304. m_inner = true;
  305. }
  306. }
  307. void FbDeleteCmd::redo()
  308. {
  309. m_element.takeFromDocument();
  310. }
  311. void FbDeleteCmd::undo()
  312. {
  313. if (m_inner) {
  314. m_parent.prependInside(m_element);
  315. } else {
  316. m_parent.appendOutside(m_element);
  317. }
  318. m_element.select();
  319. }
  320. //---------------------------------------------------------------------------
  321. // FbMoveUpCmd
  322. //---------------------------------------------------------------------------
  323. FbMoveUpCmd::FbMoveUpCmd(const FbTextElement &element)
  324. : QUndoCommand()
  325. , m_element(element)
  326. {
  327. }
  328. void FbMoveUpCmd::redo()
  329. {
  330. FbTextElement subling = m_element.previousSibling();
  331. subling.prependOutside(m_element.takeFromDocument());
  332. }
  333. void FbMoveUpCmd::undo()
  334. {
  335. FbTextElement subling = m_element.nextSibling();
  336. subling.appendOutside(m_element.takeFromDocument());
  337. }
  338. //---------------------------------------------------------------------------
  339. // FbMoveLeftCmd
  340. //---------------------------------------------------------------------------
  341. FbMoveLeftCmd::FbMoveLeftCmd(const FbTextElement &element)
  342. : QUndoCommand()
  343. , m_element(element)
  344. , m_subling(element.previousSibling())
  345. , m_parent(element.parent())
  346. {
  347. }
  348. void FbMoveLeftCmd::redo()
  349. {
  350. m_parent.appendOutside(m_element.takeFromDocument());
  351. }
  352. void FbMoveLeftCmd::undo()
  353. {
  354. if (m_subling.isNull()) {
  355. m_parent.prependInside(m_element.takeFromDocument());
  356. } else {
  357. m_subling.appendOutside(m_element.takeFromDocument());
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. // FbMoveRightCmd
  362. //---------------------------------------------------------------------------
  363. FbMoveRightCmd::FbMoveRightCmd(const FbTextElement &element)
  364. : QUndoCommand()
  365. , m_element(element)
  366. , m_subling(element.previousSibling())
  367. {
  368. }
  369. void FbMoveRightCmd::redo()
  370. {
  371. m_subling.appendInside(m_element.takeFromDocument());
  372. }
  373. void FbMoveRightCmd::undo()
  374. {
  375. m_subling.appendOutside(m_element.takeFromDocument());
  376. }