-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutFB2_0.2.ahk
287 lines (262 loc) · 8.39 KB
/
cutFB2_0.2.ahk
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
; AutoHotkey Version: 1.0.47.06
; Language: English, Russian
; Platform: Windows
; Author: Sven Karsten <[email protected]>
;
; Script Function: Cutting FB2-files to parts
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
IfExist, input.fb2
{
FileRead, Inhalt, input.fb2
OutNameNoExt = input
SelectedFile = input.fb2
}
else
{
FileSelectFile, SelectedFile, , , Open a file, eBooks (*.fb2)
if SelectedFile =
ExitApp
else
{
SplitPath, SelectedFile, , OutDir, OutExtension, OutNameNoExt
FileReadLine, Lang, %SelectedFile%, 1
FileRead, Inhalt, %SelectedFile%
SetWorkingDir %OutDir%
}
}
FileInstall, header.tmp, header.tmp
FileInstall, footer.tmp, footer.tmp
FileRead, Header, header.tmp
StringReplace, Header, Header, `r, , All
FileRead, Footer, footer.tmp
If Lang not contains utf-8,UTF-8
StringReplace, Header, Header, <?xml version="1.0" encoding="UTF-8"?>, %Lang%
Title = cutFB2 v.0.2
Mess = Cut file`, please wait...
SetTimer, RefreshTrayTip, 500
; FileDelete, *_part_*.fb2
; FileDelete, *.tmp
; Get book info
StringReplace, Inhalt, Inhalt, `r, , All
FoundPos := RegExMatch(Inhalt, "U)<title-info>(.*)</title-info>", InhaltA)
FoundPos := RegExMatch(InhaltA, "U)<book-title>(.*)</book-title>", BookTitle)
FoundPos := RegExMatch(InhaltA, "U)<author>(.*)</author>", BookAuthor)
; Find first lewel sections
StringGetPos, BodyPosition, Inhalt, </body>
Loop
{
Index = %A_Index%
StringGetPos, Position, Inhalt, section, L%Index%
If ErrorLevel = 1
Break
If Position > %BodyPosition%
Break
StringMid, Tag, Inhalt, %Position%, 8
If Tag = <section
{
Level++
SecPos%Index% := Position
SecTyp%Index% = open
SecLevel%Index% = %Level%
If SecLevel%Index% = 1
Count++
}
Else If Tag = `/section
{
Position--
SecPos%Index% := Position - 1
SecTyp%Index% = close
SecLevel%Index% = %Level%
Level--
}
SecPosIndex := SecPos%Index%
SecTypIndex := SecTyp%Index%
SecLevelIndex := SecLevel%Index%
}
Index--
Loop, %Index%
{
If SecLevel%A_Index% = 1
{
SecPosIndex := SecPos%A_Index%
If SecTyp%A_Index% = close
Inhalt := RegExReplace(Inhalt, "</section>", " ", OutputVarCount, 1, SecPosIndex)
If SecTyp%A_Index% = open
Inhalt := RegExReplace(Inhalt, "<section>", "<CutHere>", OutputVarCount, 1, SecPosIndex)
}
}
; Finde delimiter
Loop, 255
{
Delimiter := Chr(A_Index)
If Inhalt not contains %Delimiter%
Break
Delimiter := ~
}
StringReplace, Inhalt, Inhalt, <CutHere>, %Delimiter%, All
StringReplace, Inhalt, Inhalt, </body>, %Delimiter%, All
StringSplit, Content, Inhalt, %Delimiter%
Loop, %Content0%
{
ToSave := Content%A_Index%
If ToSave contains <FictionBook
FileAppend, %ToSave%, description.tmp
else If ToSave contains <body name="notes"
FileAppend, %ToSave%, notes.tmp
else If ToSave contains </FictionBook>
FileAppend, %ToSave%, binaries.tmp
else If ToSave contains <p>
{
Counter++
NewHeader = %Header%
NewBookTitle = %BookTitle%
StringReplace, NewBookTitle, NewBookTitle, </book-title>, _%Counter%</book-title>
NewHeader := RegExReplace(NewHeader, "U)<book-title>(.*)</book-title>", NewBookTitle, OutputVarCount, 1, 1)
NewHeader := RegExReplace(NewHeader, "U)<author>(.*)</author>", BookAuthor, OutputVarCount, 1, 1)
FileAppend, %NewHeader%, %OutNameNoExt%_%Counter%.fb2
FileAppend, %ToSave%, %OutNameNoExt%_%Counter%.fb2
FileAppend, %Footer%, %OutNameNoExt%_%Counter%.fb2
}
}
FileRead, Description, description.tmp
FileRead, Notes, notes.tmp
StringReplace, Notes, Notes, `r, , All
FileRead, Binaries, binaries.tmp
StringReplace, Binaries, Binaries, `r, , All
FileDelete, *.tmp
Loop, %Counter%
{
FileRead, Inhalt, %OutNameNoExt%_%A_Index%.fb2
StringReplace, Inhalt, Inhalt, `r, , All
Gosub, ImplementNotes
Gosub, ImplementBinaries
FileDelete, %OutNameNoExt%_%A_Index%.fb2
FileAppend, %Inhalt%, %OutNameNoExt%_%A_Index%.fb2
}
ExitApp
RefreshTrayTip:
Seconds++
TrayTip, %Title%, %Mess% %Seconds%, , 17
return
ImplementNotes:
IfNotInString, Inhalt, <body name="notes">
{
Mess = Correct footnotes`, please wait...
If Inhalt contains <a xmlns,<a l:href
{
; Footnotes correction
StringReplace, Inhalt, Inhalt, <a l:href, <a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href, All
FoundPos = 0
newNoteBody =
Loop
{
StringReplace, Inhalt, Inhalt, `r, , All
StartPos := FoundPos + 1
FoundPos := RegExMatch(Inhalt, "U)href([^/]*)</a>", Output, StartPos)
If FoundPos = 0
Break
; Get link
OutString = %Output%
StringReplace, OutString, OutString, %A_Space%type="note", ,
FoundPos2 := RegExMatch(OutString, "U)href=(.*)>", Link, 1)
StringReplace, Link, Link, href=`"#, ,
StringReplace, Link, Link, >, ,
; Get footnote
PosID := InStr(Notes, Link, false, 1)
; PosID++
; PosID := InStr(Notes, Link, false, PosID)
FoundPos3 := RegExMatch(Notes, "U)</title>(.*)</section>", NoteText, PosID)
StringReplace, NoteText, NoteText, </title>, ,
StringReplace, NoteText, NoteText, </section>, ,
; Create new footnote
newFootnote = <section id="%Link%>`n <title>`n <p>%A_Index%</p>`n </title>`n %NoteText%`n </section>`n
newNoteBody = %newNoteBody% %newFootnote%
}
newNoteBody = <body name="notes">`n <title>`n <p>Notes</p>`n </title>`n%newNoteBody%`n </body>
StringReplace, Inhalt, Inhalt, </body>, </body>`n%newNoteBody%
}
}
Gosub Renumber
Return
Renumber:
; Renumber footnotes
FoundPos = 0
id = %A_Now%
Loop
{
Index = %A_Index%
StartPos := FoundPos + 1
FoundPos := RegExMatch(Inhalt, "U)<a xmlns(.*)</a>", Output, StartPos)
If FoundPos = 0
Break
; Get link
OutString = %Output%
StringReplace, OutString, OutString, %A_Space%type="note", ,
FoundPos2 := RegExMatch(OutString, "U)href=(.*)>", Link, 1)
StringReplace, Link, Link, href=`"#, ,
StringReplace, Link, Link, >, ,
; Replace old link adress with new one
If Output contains %A_Space%type="note"
Replacer = xlink:href="#id%id%_%Index%">[%Index%`]</a>
Else
Replacer = xlink:href="#id%id%_%Index%" type="note">[%Index%`]</a>
Output1 := RegExReplace(Output, "xlink:href=(.*)</a>", Replacer, Count, 1, 1)
StringReplace, Inhalt, Inhalt, %Output%, %Output1%
; Search and replace second link adress in the footnote
Replacer = id%id%_%Index%`"
StringReplace, Inhalt, Inhalt, %Link%, %Replacer%
; Get begin position of the footnote
NewLink = id`=`"%Replacer%
NewLinkPos := InStr(Inhalt, NewLink, false, 1)
; Replace old footnote's number with new one
FoundPos6 := RegExMatch(Inhalt, "U)<title>(.*)</title>", NoteTitleText, NewLinkPos)
Replacer = <title>`n <p>%Index%</p>`n </title>
Inhalt := RegExReplace(Inhalt, NoteTitleText, Replacer, Count, 1, NewLinkPos)
}
Return
ImplementBinaries:
Mess = Correct pictures`, please wait...
If Inhalt contains <image xmlns, <image l
{
; Binaries correction
FoundPos = 0
newPicBody =
Loop
{
StringReplace, Inhalt, Inhalt, `r, , All
StartPos := FoundPos + 1
FoundPos := RegExMatch(Inhalt, "U)href([^/]*)/>", Link, StartPos)
If Link contains excerpt.jpg
Continue
If FoundPos = 0
Break
; Get link
StringReplace, Link, Link, href=`"#, ,
StringReplace, Link, Link, />, ,
; Get picture
PosID := InStr(Binaries, Link, false, 1)
/*
PosID++
NotLink = id`=`"anthology.jpg`"
If Notes contains %NotLink%
{
PosID := InStr(Notes, NotLink, false, PosID)
PosID++
}
PosID := InStr(Notes, Link, false, PosID)
*/
FoundPos3 := RegExMatch(Binaries, "U)content-type(.*)</binary>", PicText, PosID)
If PicText contains anthology.jpg
{
PosID++
FoundPos3 := RegExMatch(Binaries, "U)content-type(.*)</binary>", PicText, PosID)
}
; Create new binary
newBinary = <binary id="%Link% %PicText% `n
newPicBody = %newPicBody%%newBinary%
}
StringReplace, Inhalt, Inhalt, </FictionBook>, `n%newPicBody%</FictionBook>
}
Return