Browse Source

Fix header blocks

Kandrashin Denis 12 years ago
parent
commit
3129fa0ab3
8 changed files with 43 additions and 53 deletions
  1. 6 8
      source/fb2head.cpp
  2. 9 7
      source/fb2html.cpp
  3. 1 0
      source/fb2html.h
  4. 2 1
      source/fb2main.cpp
  5. 20 28
      source/fb2text.cpp
  6. 2 2
      source/fb2text.hpp
  7. 1 4
      source/js/section_get.js
  8. 2 3
      source/js/section_new.js

+ 6 - 8
source/fb2head.cpp

@@ -196,11 +196,10 @@ FbHeadItem::FbHeadItem(QWebElement &element, FbHeadItem *parent)
     , m_parent(parent)
 {
     m_name = element.tagName().toLower();
-    if (m_name == "div") {
-        QString style = element.attribute("class").toLower();
-        if (!style.isEmpty()) m_name = style;
-        if (style == "annotation") return;
-        if (style == "history") return;
+    if (m_name.left(3) == "fb:") {
+        m_name = m_name.mid(3);
+        if (m_name == "annotation") return;
+        if (m_name == "history") return;
     } else if (m_name == "img") {
         m_name = "image";
         m_text = element.attribute("alt");
@@ -217,9 +216,8 @@ FbHeadItem::~FbHeadItem()
 
 FbHeadItem * FbHeadItem::append(const QString name)
 {
-    m_element.appendInside("<div></div>");
+    m_element.appendInside(QString("<fb:%1></fb:%1>").arg(name));
     QWebElement element = m_element.lastChild();
-    element.addClass(name);
     if (name == "annotation" || name == "history") {
         element.appendInside("<p><br></p>");
     }
@@ -233,7 +231,7 @@ void FbHeadItem::addChildren(QWebElement &parent)
     QWebElement child = parent.firstChild();
     while (!child.isNull()) {
         QString tag = child.tagName().toLower();
-        if (tag == "div") {
+        if (tag.left(3) == "fb:") {
             m_list << new FbHeadItem(child, this);
         } else if (tag == "img") {
             m_list << new FbHeadItem(child, this);

+ 9 - 7
source/fb2html.cpp

@@ -110,6 +110,12 @@ bool FbTextElement::Sublist::operator !=(const FbTextElement &element) const
 //  FbTextElement
 //---------------------------------------------------------------------------
 
+QString FbTextElement::blockName() const
+{
+    QString n = tagName().toLower();
+    return n.left(3) == "fb:" ? n.mid(3) : QString();
+}
+
 QString FbTextElement::nodeName() const
 {
     QString n = tagName().toLower();
@@ -121,13 +127,9 @@ void FbTextElement::getChildren(FbElementList &list)
     FbTextElement child = firstChild();
     while (!child.isNull()) {
         QString tag = child.tagName().toLower();
-        if (tag == "fb:section") {
-            list << child;
-        } else if (tag == "fb:title") {
-            list << child;
-        } else if (tag == "fb:subtitle") {
-            list << child;
-        } else if (tag == "fb:body") {
+        if (tag == "fb:description") {
+            // skip description
+        } else if (tag.left(3) == "fb:") {
             list << child;
         } else if (tag == "img") {
             list << child;

+ 1 - 0
source/fb2html.h

@@ -60,6 +60,7 @@ public:
     FbTextElement &operator=(const QWebElement &x) { QWebElement::operator=(x); return *this; }
     FbTextElement insertInside(const QString &style, const QString &html);
     FbTextElement child(int index) const;
+    QString blockName() const;
     QString nodeName() const;
     void getChildren(FbElementList &list);
     bool hasSubtype(const QString &style) const;

+ 2 - 1
source/fb2main.cpp

@@ -780,7 +780,6 @@ void FbMainWindow::viewText()
         textFrame = new FbTextFrame(this, actionInspect);
     }
     setCentralWidget(textFrame);
-    textFrame->view()->setFocus();
     viewTree();
 
     FbTextEdit *textEdit = textFrame->view();
@@ -794,6 +793,8 @@ void FbMainWindow::viewText()
     actionContents->setEnabled(true);
     actionPictures->setEnabled(true);
     actionInspect->setEnabled(true);
+
+    textFrame->view()->setFocus();
 }
 
 void FbMainWindow::viewHead()

+ 20 - 28
source/fb2text.cpp

@@ -112,14 +112,13 @@ bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest
     if (type == NavigationTypeLinkClicked) {
         qCritical() << request.url().fragment();
         return false;
-//        QToolTip::showText(request.url().fragment());
     }
     return QWebPage::acceptNavigationRequest(frame, request, type);
 }
 
-QString FbTextPage::div(const QString &style, const QString &text)
+QString FbTextPage::block(const QString &name, const QString &text)
 {
-    return QString("<div class=%1>%2</div>").arg(style).arg(text);
+    return QString("<fb:%1>%2</fb:%1>").arg(name).arg(text);
 }
 
 QString FbTextPage::p(const QString &text)
@@ -152,7 +151,7 @@ void FbTextPage::update()
 
 void FbTextPage::appendSection(const FbTextElement &parent)
 {
-    QString html = div("section", div("title", p()) + p());
+    QString html = block("section", block("title", p()) + p());
     FbTextElement element = parent;
     element.appendInside(html);
     element = parent.lastChild();
@@ -162,7 +161,7 @@ void FbTextPage::appendSection(const FbTextElement &parent)
 
 void FbTextPage::insertBody()
 {
-    QString html = div("body", div("title", p()) + div("section", div("title", p()) + p()));
+    QString html = block("body", block("title", p()) + block("section", block("title", p()) + p()));
     FbTextElement element = body();
     element.appendInside(html);
     element = element.lastChild();
@@ -188,7 +187,7 @@ void FbTextPage::insertTitle()
     while (!element.isNull()) {
         FbTextElement parent = element.parent();
         if ((parent.isSection() || parent.isBody()) && !parent.hasTitle()) {
-            QString html = div("title", p());
+            QString html = block("title", p());
             parent.prependInside(html);
             element = parent.firstChild();
             QUndoCommand * command = new FbInsertCmd(element);
@@ -205,7 +204,7 @@ void FbTextPage::insertSubtitle()
     while (!element.isNull()) {
         FbTextElement parent = element.parent();
         if (parent.isSection()) {
-            QString html = div("subtitle", p());
+            QString html = block("subtitle", p());
             if (element.isTitle()) {
                 element.appendOutside(html);
                 element = element.nextSibling();
@@ -227,7 +226,7 @@ void FbTextPage::insertPoem()
     while (!element.isNull()) {
         FbTextElement parent = element.parent();
         if (parent.isSection()) {
-            QString html = div("poem", div("stanza", p()));
+            QString html = block("poem", block("stanza", p()));
             if (element.isTitle()) {
                 element.appendOutside(html);
                 element = element.nextSibling();
@@ -248,7 +247,7 @@ void FbTextPage::insertStanza()
     FbTextElement element = current();
     while (!element.isNull()) {
         if (element.isStanza()) {
-            QString html = div("stanza", p());
+            QString html = block("stanza", p());
             element.appendOutside(html);
             element = element.nextSibling();
             QUndoCommand * command = new FbInsertCmd(element);
@@ -273,7 +272,7 @@ void FbTextPage::insertEpigraph()
     FbTextElement element = current();
     while (!element.isNull()) {
         if (element.hasSubtype(type)) {
-            QString html = div("epigraph", p());
+            QString html = block("epigraph", p());
             element = element.insertInside(type, html);
             QUndoCommand * command = new FbInsertCmd(element);
             push(command, tr("Insert epigraph"));
@@ -289,21 +288,11 @@ void FbTextPage::insertDate()
 
 void FbTextPage::insertText()
 {
-    FbTextElement element = current();
-    while (!element.isNull()) {
-        if (element.tagName() == "DIV") {
-            if (element.parent().isBody()) break;
-            element.appendOutside("<p><br></p>");
-            element.nextSibling().select();
-            return;
-        }
-        element = element.parent();
-    }
 }
 
-void FbTextPage::createDiv(const QString &className)
+void FbTextPage::createBlock(const QString &name)
 {
-    QString style = className;
+    QString style = name;
     QString js1 = jScript("section_get.js");
     QString result = mainFrame()->evaluateJavaScript(js1).toString();
     QStringList list = result.split("|");
@@ -315,15 +304,15 @@ void FbTextPage::createDiv(const QString &className)
     FbTextElement duplicate = original.clone();
     original.appendOutside(duplicate);
     original.takeFromDocument();
-    QString js2 = jScript("section_new.js") + ";f(this,'%1',%2)";
+    QString js2 = jScript("section_new.js") + ";f(this,'fb:%1',%2)";
     duplicate.evaluateJavaScript(js2.arg(style).arg(position));
     QUndoCommand * command = new FbReplaceCmd(original, duplicate);
-    push(command, tr("Create <%1>").arg(className));
+    push(command, tr("Create <%1>").arg(style));
 }
 
 void FbTextPage::createSection()
 {
-    createDiv("section");
+    createBlock("section");
 }
 
 void FbTextPage::deleteSection()
@@ -358,7 +347,7 @@ void FbTextPage::deleteSection()
 
 void FbTextPage::createTitle()
 {
-    createDiv("title");
+    createBlock("title");
 }
 
 FbTextElement FbTextPage::current()
@@ -398,10 +387,13 @@ QString FbTextPage::status()
 
 void FbTextPage::loadFinished()
 {
-    mainFrame()->evaluateJavaScript("window.focus()");
     mainFrame()->addToJavaScriptWindowObject("logger", &m_logger);
-    FbTextElement element = body().findFirst("div.body");
+    FbTextElement element = body().findFirst("fb\\:section");
+    if (element.isNull()) element = body().findFirst("fb\\:body");
     if (element.isNull()) element = body();
+    FbTextElement child = element.firstChild();
+    if (child.isTitle()) child = child.nextSibling();
+    if (!child.isNull()) element = child;
     element.select();
 }
 

+ 2 - 2
source/fb2text.hpp

@@ -109,10 +109,10 @@ public slots:
 
 protected:
     virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
-    void createDiv(const QString &className);
+    void createBlock(const QString &name);
 
 protected:
-    static QString div(const QString &style, const QString &text);
+    static QString block(const QString &name, const QString &text);
     static QString p(const QString &text = "<br/>");
     void update();
 

+ 1 - 4
source/js/section_get.js

@@ -9,10 +9,7 @@ while (true) {
  if(root===null)return;
  tag=root.tagName;
  if(tag==="BODY")return;
- if(tag==="DIV"){
-  type=root.className.toLowerCase();
-  if(type==="body"||type==="section")break;
- }
+ if(tag==="FB:BODY"||type==="FB:SECTION")break;
  root = root.parentNode;
 }
 while(start.parentNode!==root) {

+ 2 - 3
source/js/section_new.js

@@ -1,11 +1,10 @@
-f=function(elem,style,start,end){
+f=function(elem,tag,start,end){
 start=$(elem).children().get(start);
 end=$(elem).children().get(end);
 var range=document.createRange();
 range.setStartBefore(start);
 range.setEndAfter(end);
-var newNode=document.createElement("div");
-newNode.className=style;
+var newNode=document.createElement(tag);
 range.surroundContents(newNode);
 range.setStartBefore(start);
 range.setEndBefore(start);