1
0

fb2html.cpp 11 KB

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