-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencontent.cpp
More file actions
82 lines (66 loc) · 2.59 KB
/
encontent.cpp
File metadata and controls
82 lines (66 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "encontent.h"
#include "encontentparser.h"
#include <QDebug>
EnContent::EnContent(const QString& content){
mNoteContent = content;
cutTag(mNoteContent, "en-note");
}
QString& EnContent::cutTag(QString& src, const QString& tag){
int indexBegin = src.indexOf('<' + tag);
indexBegin = src.indexOf('>', indexBegin);
int indexEnd = src.lastIndexOf("</" + tag + ">");
if(indexBegin >= 0 && indexEnd >= 0 && indexBegin < indexEnd){
src.remove(indexEnd, src.length() - indexEnd);
src.remove(0, indexBegin + 1);
}else{
qWarning("EnNoteContent::cutBetween: wrong string or params");
}
//qDebug() << src;
return src;
}
/*
void EnContent::processMedias(){
EnContentParser handler;
QXmlInputSource source;
source.setData(mNoteContent);
QXmlSimpleReader reader;
reader.setContentHandler(&handler);
reader.parse(source);
}
*/
void EnContent::processMedias(){
// ïîçèöèÿ òåãà en-media
int mediaIndex = mNoteContent.indexOf("<en-media");
// âûïîëíÿåì äëÿ êàæäîãî òåêà en-media
while(mediaIndex > 0){
// ïîçèöèÿ êîíöà îòêðûâàþùåãî òåãà
int mediaIndexEnd = mNoteContent.indexOf('>', mediaIndex);
// ïîëó÷àåì òèï è õåø ýëåìåíòà
QString hash = getAttributeValueFromTag(mNoteContent, mediaIndex, mediaIndexEnd, "hash");
QString type = getAttributeValueFromTag(mNoteContent, mediaIndex, mediaIndexEnd, "type");
// åñëè òèï - èçîáðàæåíèå
if(type.startsWith("image") && !hash.isEmpty()){
// ïðîâåðÿåì îäèíî÷íûé ëè òåã, åñëè íåò òî ìåíÿåì çàêðûâàþùèé òåã
if(mNoteContent[mediaIndexEnd - 1] != '/'){
int mediaCloseTagIndex = mNoteContent.indexOf("</en-media>", mediaIndexEnd);
mNoteContent.replace(mediaCloseTagIndex, 11, "</img>");
}
// ìåíÿåì íàçâàíèå îòêðûâàþùåãî òåãà
mNoteContent.replace(mediaIndex, 9, "<img src=\"c:/TEMP/zothReader/img/" + hash + '.' + type.mid(6) + '"' );
}
mediaIndex = mNoteContent.indexOf("<en-media", mediaIndexEnd);
}
//qDebug() << mNoteContent;
}
QString EnContent::getAttributeValueFromTag(const QString& src,
int tagBegin,
int tagEnd,
const QString& attribute)
{
int begin = src.indexOf(attribute, tagBegin) + attribute.length();
if(begin > tagEnd)
return QString();
begin = 1 + src.indexOf('"', begin);
int end = src.indexOf('"', begin);
return src.mid(begin, end - begin);
}