Browse Source

refactor DocType

Le Tan 8 years ago
parent
commit
6a10c7ab3c

+ 2 - 1
src/dialog/vfindreplacedialog.cpp

@@ -294,9 +294,10 @@ void VFindReplaceDialog::updateState(DocType p_docType, bool p_editMode)
     if (p_editMode || p_docType == DocType::Html) {
         m_wholeWordOnlyCheck->setEnabled(true);
         m_regularExpressionCheck->setEnabled(true);
-    } else {
+    } else if (p_docType == DocType::Markdown) {
         m_wholeWordOnlyCheck->setEnabled(false);
         m_regularExpressionCheck->setEnabled(false);
     }
+
     m_replaceAvailable = p_editMode;
 }

+ 20 - 16
src/utils/vutils.cpp

@@ -116,22 +116,6 @@ void VUtils::processStyle(QString &style, const QVector<QPair<QString, QString>
     }
 }
 
-bool VUtils::isMarkdown(const QString &p_fileName)
-{
-    const QVector<QString> mdPostfix({"md", "markdown", "mkd"});
-
-    QFileInfo info(p_fileName);
-    QString suffix = info.suffix();
-
-    for (int i = 0; i < mdPostfix.size(); ++i) {
-        if (suffix == mdPostfix[i]) {
-            return true;
-        }
-    }
-
-    return false;
-}
-
 QString VUtils::fileNameFromPath(const QString &p_path)
 {
     if (p_path.isEmpty()) {
@@ -457,3 +441,23 @@ void VUtils::sleepWait(int p_milliseconds)
         QCoreApplication::processEvents();
     }
 }
+
+DocType VUtils::docTypeFromName(const QString &p_name)
+{
+    static QMap<int, QVector<QString>> suffixes;
+
+    if (suffixes.isEmpty()) {
+        suffixes[(int)DocType::Markdown] = {"md", "markdown", "mkd"};
+        suffixes[(int)DocType::List] = {"ls", "list"};
+        suffixes[(int)DocType::Container] = {"co", "container", "con"};
+    }
+
+    QString suf = QFileInfo(p_name).suffix().toLower();
+    for (auto it = suffixes.begin(); it != suffixes.end(); ++it) {
+        if (it.value().contains(suf)) {
+            return DocType(it.key());
+        }
+    }
+
+    return DocType::Html;
+}

+ 3 - 1
src/utils/vutils.h

@@ -53,7 +53,6 @@ public:
     static QString generateCopiedFileName(const QString &p_dirPath, const QString &p_fileName);
     static QString generateCopiedDirName(const QString &p_parentDirPath, const QString &p_dirName);
     static void processStyle(QString &style, const QVector<QPair<QString, QString> > &varMap);
-    static bool isMarkdown(const QString &p_fileName);
 
     // Return the last directory name of @p_path.
     static inline QString directoryNameFromPath(const QString& p_path);
@@ -94,6 +93,9 @@ public:
 
     static void sleepWait(int p_milliseconds);
 
+    // Return the DocType according to suffix.
+    static DocType docTypeFromName(const QString &p_name);
+
     // Regular expression for image link.
     // ![image title]( http://github.com/tamlok/vnote.jpg "alt \" text" )
     // Captured texts (need to be trimmed):

+ 5 - 1
src/vconstants.h

@@ -3,8 +3,12 @@
 
 // Html: rich text file;
 // Markdown: Markdown text file;
-enum class DocType { Html, Markdown };
+// List: Infinite list file like WorkFlowy;
+// Container: a composite file containing multiple files;
+enum class DocType { Html, Markdown, List, Container, Invalid };
 
+// Normal: note file managed by VNote;
+// Orphan: external file;
 enum class FileType { Normal, Orphan };
 
 enum class ClipboardOpType { Invalid, CopyFile, CopyDir };

+ 1 - 1
src/vdirectory.cpp

@@ -490,7 +490,7 @@ VFile *VDirectory::copyFile(VDirectory *p_destDir, const QString &p_destName,
 
     VDirectory *srcDir = p_srcFile->getDirectory();
     DocType docType = p_srcFile->getDocType();
-    DocType newDocType = VUtils::isMarkdown(destPath) ? DocType::Markdown : DocType::Html;
+    DocType newDocType = VUtils::docTypeFromName(destPath);
 
     QVector<ImageLink> images;
     if (docType == DocType::Markdown) {

+ 3 - 0
src/vedittab.cpp

@@ -93,6 +93,7 @@ void VEditTab::setupUI()
         addWidget(m_textEditor);
         webPreviewer = NULL;
         break;
+
     default:
         qWarning() << "unknown doc type" << int(m_file->getDocType());
         Q_ASSERT(false);
@@ -122,6 +123,7 @@ void VEditTab::showFileReadMode()
     case DocType::Html:
         m_textEditor->setReadOnly(true);
         break;
+
     case DocType::Markdown:
         if (mdConverterType == MarkdownConverterType::Hoedown) {
             previewByConverter();
@@ -133,6 +135,7 @@ void VEditTab::showFileReadMode()
         clearSearchedWordHighlight();
         scrollPreviewToHeader(outlineIndex);
         break;
+
     default:
         qWarning() << "unknown doc type" << int(m_file->getDocType());
         Q_ASSERT(false);

+ 2 - 3
src/vfile.cpp

@@ -9,7 +9,7 @@
 VFile::VFile(const QString &p_name, QObject *p_parent,
              FileType p_type, bool p_modifiable)
     : QObject(p_parent), m_name(p_name), m_opened(false), m_modified(false),
-      m_docType(VUtils::isMarkdown(p_name) ? DocType::Markdown : DocType::Html),
+      m_docType(VUtils::docTypeFromName(p_name)),
       m_type(p_type), m_modifiable(p_modifiable)
 {
 }
@@ -25,7 +25,6 @@ bool VFile::open()
         return true;
     }
     Q_ASSERT(m_content.isEmpty());
-    Q_ASSERT(m_docType == (VUtils::isMarkdown(m_name) ? DocType::Markdown : DocType::Html));
     QString path = retrivePath();
     qDebug() << "path" << path;
     m_content = VUtils::readFileFromDisk(path);
@@ -116,7 +115,7 @@ void VFile::deleteLocalImages()
 void VFile::setName(const QString &p_name)
 {
     m_name = p_name;
-    DocType newType = VUtils::isMarkdown(p_name) ? DocType::Markdown : DocType::Html;
+    DocType newType = VUtils::docTypeFromName(p_name);
     if (newType != m_docType) {
         qWarning() << "setName() change the DocType. A convertion should be followed";
     }

+ 2 - 1
src/vfilelist.cpp

@@ -462,9 +462,10 @@ bool VFileList::copyFile(VDirectory *p_destDir, const QString &p_destName, VFile
     if (srcPath == destPath) {
         return true;
     }
+
     // If change the file type, we need to close it first
     DocType docType = p_file->getDocType();
-    DocType newDocType = VUtils::isMarkdown(destPath) ? DocType::Markdown : DocType::Html;
+    DocType newDocType = VUtils::docTypeFromName(destPath);
     if (docType != newDocType) {
         if (editArea->isFileOpened(p_file)) {
             int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"),

+ 1 - 1
src/vmdedit.cpp

@@ -17,7 +17,7 @@ VMdEdit::VMdEdit(VFile *p_file, VDocument *p_vdoc, MarkdownConverterType p_type,
                  QWidget *p_parent)
     : VEdit(p_file, p_parent), m_mdHighlighter(NULL)
 {
-    Q_ASSERT(p_file->getDocType() == DocType::Markdown);
+    V_ASSERT(p_file->getDocType() == DocType::Markdown);
 
     setAcceptRichText(false);
     m_mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(),

+ 0 - 1
src/vorphanfile.cpp

@@ -18,7 +18,6 @@ bool VOrphanFile::open()
         return true;
     }
     Q_ASSERT(m_content.isEmpty());
-    Q_ASSERT(m_docType == (VUtils::isMarkdown(m_name) ? DocType::Markdown : DocType::Html));
     Q_ASSERT(QFileInfo::exists(m_path));
     m_content = VUtils::readFileFromDisk(m_path);
     m_modified = false;