Top Level Namespace

Defined Under Namespace

Modules: Command, CommandLine, FileUtils, Helper, Inventory, Narou, WinAPI Classes: ConverterBase, Database, Device, DiffViewer, Downloader, File, HTML, Illustration, Ini, Inspector, Mailer, NovelConverter, NovelInfo, NovelSetting, ProgressBar, SectionStripper, SiteSetting, String, StripException, Template, WebSocket, WebSocketServer

Constant Summary collapse

Version =

Copyright 2013 whiteleaf. All rights reserved.

"2.6.1"
KINDLESTRIP_VERSION =

! ruby -*- coding: utf-8 -*-

It was translated into Ruby script by whiteleaf.

original source code: kindlestrip.py v.1.35 www.mobileread.com/forums/showthread.php?t=96903

This script strips the penultimate record from a Mobipocket file. This is useful because the current KindleGen add a compressed copy of the source files used in this record, making the ebook produced about twice as big as it needs to be.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <unlicense.org/>

Written by Paul Durrant, 2010-2011, [email protected], pdurrant on mobileread.com With enhancements by Kevin Hendricks, KevinH on mobileread.com

Changelog

1.00 - Initial version
1.10 - Added an option to output the stripped data
1.20 - Added check for source files section (thanks Piquan)
1.30 - Added prelim Support for K8 style mobis
1.31 - removed the SRCS section but kept a 0 size entry for it
1.32 - removes the SRCS section and its entry, now updates metadata 121 if needed
1.33 - now uses and modifies mobiheader SRCS and CNT
1.34 - added credit for Kevin Hendricks
1.35 - fixed bug when more than one compilation (SRCS/CMET) records
'1.35'
BlankConverter =
Class.new(ConverterBase) {}

Instance Method Summary collapse

Instance Method Details

#converter(title, &block) ⇒ Object



13
14
15
# File 'lib/loadconverter.rb', line 13

def converter(title, &block)
  $converter_container[title] = Class.new(ConverterBase, &block)
end

#error(str) ⇒ Object



158
159
160
# File 'lib/logger.rb', line 158

def error(str)
  $stdout.error str
end

#load_converter(title, archive_path) ⇒ Object

小説固有コンバーターのロード

ファイル名は converter.rb 固定で、変換処理のフックを定義できる。 基本の変換処理の実行前にはbeforeメソッド、変換処理後にafterメソッドが呼ばれる。 このファイルを必ず用意する必要はない。 converter は ConverterBase を継承したクラスを生成するものである。 converter “タイトル” {} は class CONVERTER < ConverterBase; end にほぼ等しい。

デフォルトの動作は ConverterBase#before 及び ConverterBase#aftert に定義されている。 super を呼ばなければ基本動作を抑制出来る。

引数の io は StringIO のオブジェクトであり、ファイル先頭に seek(rewind) してあることが保証される。 また、返却する IO オブジェクトはファイル先頭に seek しておく必要はない。 text_type は渡されるテキストがタイトルなのか、前書きなのか等の種別を判断する文字列が渡される。 渡される文字列と意味:

story         あらすじ
chapter       章のタイトル
subtitle      節のタイトル
introduction  前書き
body          本文
postscript    後書き
textfile      テキストファイル形式で変換をしようとした場合

e.g.) converter “書籍のタイトル” do

# 共通変換処理の前に呼ばれる
def before(io, text_type)
  io.string.gsub!("\n\n", "\n")   # WEB小説に多い無駄な空改行を削除する
  io                              # 返り値もStringIOで
end

# 共通変換処理の後に呼ばれる
def after(io, text_type)
  buffer = StringIO.new
  io.each do |line|
    ~何らかの処理~(共通変換処理で漢数字化されたものを特定部分だけアラビア数字に戻したり)
    buffer.write ~~
  end
  buffer
end

end



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/loadconverter.rb', line 61

def load_converter(title, archive_path)
  converter_path = File.join(archive_path, "converter.rb")
  if File.exist?(converter_path)
    eval(File.read(converter_path, mode: "r:BOM|UTF-8"), binding, converter_path)
  else
    return BlankConverter
  end
  conv = $converter_container[title] || $converter_container[File.basename(archive_path)]
  if conv
    return conv
  else
    title_for_converter = (title =~ /.txt\z/ ? title : File.basename(archive_path))
    error "converter.rbは見つかりましたが、`converter'で登録されていないようです。" +
          "変換処理は converter \"#{title_for_converter.gsub('"', '\\"')}\" として登録する必要があります"
    return BlankConverter
  end
end

#warn(str) ⇒ Object



154
155
156
# File 'lib/logger.rb', line 154

def warn(str)
  $stdout.warn str
end

#write_color(str, console = STDOUT) ⇒ Object



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
# File 'lib/color.rb', line 69

def write_color(str, console = STDOUT)
  @decoration ||= 0
  @foreground ||= $default_foreground
  @background ||= $default_background
  @reverse ||= false
  str.gsub("\xef\xbd\x9e", "\xe3\x80\x9c").split(/(\e\[\d*[a-zA-Z])/).each do |token|
    case token
    when /\e\[(\d+)m/
      code = $1.to_i
      code = code > 90 ? (code % 60) : code
      case code
      when 0
        @decoration = 0
        @foreground = $default_foreground
        @background = $default_background
        @reverse = false
      when 1
        @decoration |= $colorMap[1]
      when 4
        @decoration |= $colorMap[4]
      when 7
        @reverse = true
      when 30..39
        @foreground = $colorMap[code].to_i
      when 40..49
        @background = $colorMap[code].to_i
      end
      foreground = @foreground
      background = @background
      if @reverse
        foreground <<= 4
        background >>= 4
        # reverse では intensity 情報も文字と背景で入れ替わるでの、
        # 下位4bitと上位4bitをスワップ
        @decoration = @decoration & 0xff00 | @decoration >> 4 & 0xf | (@decoration & 0xf) << 4
      end
      loop do
        error_code = WinAPI.SetConsoleTextAttribute $hStdOut, @decoration | foreground | background
        if error_code == 0
          if WinAPI.GetLastError == 6
            $hStdOut = WinAPI.GetStdHandle(0xFFFFFFF5)
            redo
          end
        end
        break
      end
    when /\e\[\d*[a-zA-Z]/, ""
      # do nothing
    else
      console.write token
    end
  end
  WinAPI.SetConsoleTextAttribute $hStdOut, $oldColor
end