|
|
发表于 2025-7-23 14:30:25
|
显示全部楼层
本帖最后由 flashercs 于 2025-7-23 18:54 编辑
- <#*,:
- @echo off
- cd /d "%~dp0"
- set "batchfile=%~f0"
- Powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- # 合并xhtml
- $utf8 = New-Object System.Text.UTF8Encoding -ArgumentList $false
- Add-Type -AssemblyName System.Xml.Linq
- if (-not $?) { return }
- [System.Xml.Linq.XNamespace]$ns_opf = "http://www.idpf.org/2007/opf"
- $dic_item = New-Object 'System.Collections.Generic.Dictionary[string,object]' -ArgumentList ([System.StringComparer]::OrdinalIgnoreCase)
- $dic_fileName = New-Object 'System.Collections.Generic.Dictionary[string,object]' -ArgumentList ([System.StringComparer]::OrdinalIgnoreCase)
- $strb = New-Object System.Text.StringBuilder -ArgumentList 4kb
- $re_body = [regex]'(?s)(<body[^>]*>)(.*)(</body>)'
- $re_src = [regex]'(src=")([^"]+)'
- foreach ($di0 in Get-ChildItem -Path '.\epub解包后\*' | Where-Object { $_ -is [IO.DirectoryInfo] }) {
- $di0.Name
- $opf = Get-ChildItem -LiteralPath $di0.FullName -Filter content.opf -Recurse | Where-Object { $_ -is [IO.FileInfo] } | Select-Object -First 1
- if ($null -eq $opf) { "找不到文件: content.opf"; continue }
- $ncx = Get-ChildItem -LiteralPath $di0.FullName -Filter toc.ncx -Recurse | Where-Object { $_ -is [IO.FileInfo] } | Select-Object -First 1
- if ($null -eq $ncx) { "找不到文件: toc.ncx"; continue }
- # $diText = Get-ChildItem -LiteralPath $di0.FullName -Filter Text -Recurse | Where-Object { $_ -is [IO.DirectoryInfo] } | Select-Object -First 1
- # if ($null -eq $diText) { "找不到文件夹: Text"; continue }
- # 修改 opf
- "修改 opf:$($opf.FullName)"
- try {
- $dic_item.Clear()
- $dic_fileName.Clear()
- $xdoc_opf = [System.Xml.Linq.XDocument]::Load($opf.FullName)
- $dir_opf = [IO.Path]::GetDirectoryName($opf.FullName)
- $xel_spine = $xdoc_opf.Root.Element($ns_opf + 'spine')
- foreach ($xel_item in $xdoc_opf.Root.Element($ns_opf + 'manifest').Elements($ns_opf + 'item')) {
- $xattr_href = $xel_item.Attribute('href')
- if ($xattr_href) {
- $baseName = [IO.Path]::GetFileNameWithoutExtension($xattr_href.Value)
- if ($baseName -match '^(.+)_split_(.+)$') {
- $prefix = $Matches[1]
- if (-not $dic_item.ContainsKey($prefix)) {
- $dic_item[$prefix] = New-Object 'System.Collections.Generic.List[System.Xml.Linq.XElement]'
- }
- $dic_item[$prefix].Add($xel_item)
- }
- }
- }
- if ($dic_item.Count -gt 0) {
- foreach ($prefix in $dic_item.Keys) {
- $strb.Length = 0
- for ($i = 0; $i -lt $dic_item[$prefix].Count; ++$i) {
- $xel_item = $dic_item[$prefix][$i]
- $html_path = [IO.Path]::GetFullPath([IO.Path]::Combine($dir_opf, $xel_item.Attribute('href').Value))
- $html_text = [IO.File]::ReadAllText($html_path)
- if ($i -eq 0) {
- $xel_item0 = $xel_item
- $html_path0 = $html_path
- $html_text0 = $html_text
- $ext0 = [IO.Path]::GetExtension($html_path0)
- } else {
- # 删除 spine/itemref
- $item_id = $xel_item.Attribute('id').Value
- foreach ($xel_itemref in @($xel_spine.Elements($ns_opf + 'itemref'))) {
- if ($xel_itemref.Attribute('idref').Value -ceq $item_id) {
- $xel_itemref.Remove()
- }
- }
- # 删除 manifest/item
- $xel_item.Remove()
- # 删除 xhtml
- [IO.File]::Delete($html_path)
- }
- $m = $re_body.Match($html_text)
- if ($m.Success) {
- $null = $strb.Append($m.Groups[2].Value)
- }
-
- }
- $html_text_new = $re_body.Replace($html_text0, {
- param($m)
- $m.Groups[1].Value + $strb.ToString() + $m.Groups[3].Value
- })
- # 保存001.xhtml
- $html_path_new = [IO.Path]::Combine([IO.Path]::GetDirectoryName($html_path0), $prefix + $ext0)
- [IO.File]::WriteAllText($html_path_new, $html_text_new)
- [IO.File]::Delete($html_path0)
- # 更改 $xel_item0 属性 href
- $xel_item0.Attribute('href').Value = $html_path_new.Substring($dir_opf.Length).TrimStart('\').Replace('\', '/')
- }
- $sw = New-Object System.IO.StreamWriter -ArgumentList ($opf.FullName, $false, $utf8)
- $xdoc_opf.Save($sw, 'OmitDuplicateNamespaces')
- $sw.Close()
- }
- # 获取文件列表
- foreach ($xel_item in $xdoc_opf.Root.Element($ns_opf + 'manifest').Elements($ns_opf + 'item')) {
- $xattr_href = $xel_item.Attribute('href')
- if ($xattr_href) {
- $fileName = [IO.Path]::GetFileName($xattr_href.Value)
- $dic_fileName.$fileName = [IO.Path]::GetFullPath([IO.Path]::Combine($dir_opf, $xattr_href.Value))
- }
- }
- } finally {}
- # 修改 ncx
- "修改 ncx:$($ncx.FullName)"
- try {
- $dir_ncx = [IO.Path]::GetDirectoryName($ncx.FullName)
- $str_ncx = [IO.File]::ReadAllText($ncx.FullName)
- $str_ncx = $re_src.Replace($str_ncx, {
- param($m)
- $gp1 = $m.Groups[1].Value
- $gp2 = $m.Groups[2].Value
- $fileName = ($gp2 -split '/')[-1]
- $fileName0 = ($fileName -split '#')[0]
- if ($dic_fileName.ContainsKey($fileName0)) {
- $relpath = [IO.Path]::Combine([IO.Path]::GetDirectoryName($dic_fileName.$fileName0.Substring($dir_ncx.Length).TrimStart('\')), $fileName).Replace('\', '/')
- } else {
- $relpath = $gp2
- }
- $gp1 + $relpath
- })
- [IO.File]::WriteAllText($ncx.FullName, $str_ncx)
- } finally {
-
- }
-
- trap {}
- }
复制代码 |
评分
-
查看全部评分
|