Module: Softcover::Utils
- Extended by:
- Utils
- Included in:
- Softcover, Book, BookManifest, Builder, CLI, Client, Commands::Deployment, Commands::EpubValidator, Commands::Publisher, Commands::Server, Uploader, Utils
- Defined in:
- lib/softcover/utils.rb
Constant Summary collapse
- UNITS =
%W(B KB MB GB TB).freeze
Instance Method Summary collapse
-
#add_highlight_class!(pygments_css) ⇒ Object
Adds a ‘highlight’ class for MathJax compatibility.
- #as_size(number) ⇒ Object
- #current_book ⇒ Object
-
#digest(string) ⇒ Object
Returns a digest of the string.
-
#executable(filename, message) ⇒ Object
Returns the executable if it exists, raising an error otherwise.
-
#execute(command) ⇒ Object
Execute a command.
- #in_book_directory? ⇒ Boolean
- #logged_in? ⇒ Boolean
- #mkdir(dir) ⇒ Object
-
#path(path_string) ⇒ Object
Returns the system-independent file path.
- #reset_current_book! ⇒ Object
- #rm(file) ⇒ Object
- #silence ⇒ Object
-
#source ⇒ Object
Returns the source type (PolyTeX or Markdown) of the current book.
-
#tmpify(manifest, filename) ⇒ Object
Returns the tmp version of a filename.
-
#write_pygments_file(format, path = '.') ⇒ Object
Writes a Pygments style file.
Instance Method Details
#add_highlight_class!(pygments_css) ⇒ Object
Adds a ‘highlight’ class for MathJax compatibility.
89 90 91 |
# File 'lib/softcover/utils.rb', line 89 def add_highlight_class!(pygments_css) pygments_css.gsub!(/^/, '.highlight ') end |
#as_size(number) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/softcover/utils.rb', line 41 def as_size(number) if number.to_i < 1024 exponent = 0 else max_exp = UNITS.size - 1 exponent = ( Math.log( number ) / Math.log( 1024 ) ).to_i exponent = max_exp if exponent > max_exp number /= 1024 ** exponent end "#{number.round} #{UNITS[ exponent ]}" end |
#current_book ⇒ Object
4 5 6 7 8 9 |
# File 'lib/softcover/utils.rb', line 4 def current_book # using module level variable because it should be context independent @@current_book ||= begin in_book_directory? ? Softcover::Book.new(origin: source) : false end end |
#digest(string) ⇒ Object
Returns a digest of the string.
94 95 96 |
# File 'lib/softcover/utils.rb', line 94 def digest(string) Digest::SHA1.hexdigest(string) end |
#executable(filename, message) ⇒ Object
Returns the executable if it exists, raising an error otherwise.
99 100 101 102 103 104 105 106 |
# File 'lib/softcover/utils.rb', line 99 def executable(filename, ) filename.tap do |f| unless File.exist?(f) $stderr.puts exit 1 end end end |
#execute(command) ⇒ Object
Execute a command. The issue here is that ‘exec` is awful in tests, since it exits the process. This command arranges to use `system` in tests instead.
126 127 128 |
# File 'lib/softcover/utils.rb', line 126 def execute(command) Softcover.test? ? system(command) : exec(command) end |
#in_book_directory? ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/softcover/utils.rb', line 20 def in_book_directory? Softcover::BookManifest::find_book_root! files = Dir['**/*'] Softcover::FORMATS.each do |format| unless files.any?{ |file| File.extname(file) == ".#{format}" } puts "No #{format} found, skipping." end end return Softcover::BookManifest::valid_directory? end |
#logged_in? ⇒ Boolean
34 35 36 37 |
# File 'lib/softcover/utils.rb', line 34 def logged_in? require 'softcover/config' Softcover::Config['api_key'].present? end |
#mkdir(dir) ⇒ Object
108 109 110 |
# File 'lib/softcover/utils.rb', line 108 def mkdir(dir) Dir.mkdir(dir) unless File.directory?(dir) end |
#path(path_string) ⇒ Object
Returns the system-independent file path. It’s nicer to write ‘path(’foo/bar/baz’)‘ than `File.join(’foo’, ‘bar’, ‘baz’)‘.
119 120 121 |
# File 'lib/softcover/utils.rb', line 119 def path(path_string) File.join(*path_string.split('/')) end |
#reset_current_book! ⇒ Object
16 17 18 |
# File 'lib/softcover/utils.rb', line 16 def reset_current_book! @@current_book = nil end |
#rm(file) ⇒ Object
112 113 114 |
# File 'lib/softcover/utils.rb', line 112 def rm(file) FileUtils.rm(file) if File.exist?(file) end |
#silence ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/softcover/utils.rb', line 130 def silence return yield if ENV['silence'] == 'false' silence_stream(STDOUT) do yield end end |
#source ⇒ Object
Returns the source type (PolyTeX or Markdown) of the current book.
12 13 14 |
# File 'lib/softcover/utils.rb', line 12 def source Dir.glob(path('chapters/*.md')).empty? ? :polytex : :markdown end |
#tmpify(manifest, filename) ⇒ Object
Returns the tmp version of a filename. E.g., tmpify(‘foo.tex’) => ‘foo.tmp.tex’
59 60 61 62 63 64 |
# File 'lib/softcover/utils.rb', line 59 def tmpify(manifest, filename) mkdir 'tmp' sep = File::SEPARATOR filename.sub(manifest.polytex_dir + sep, 'tmp' + sep). sub('.tex', '.tmp.tex') end |
#write_pygments_file(format, path = '.') ⇒ Object
Writes a Pygments style file. We support both :html (outputting CSS) and :latex (outputting a LaTeX style file).
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/softcover/utils.rb', line 69 def write_pygments_file(format, path = '.') extension = case format when :html 'css' when :latex 'sty' end # Here we burrow into the private 'Pygments#mentos' method. # Pygments exposes a 'css' method to return the CSS, # but we want to be able to output a LaTeX style file as well. # The inclusion of the ':css' symbol is necessary but doesn't actually # result in CSS being output unless the format is 'html'. pygments = Pygments.send(:mentos, :css, [format.to_s, '']) add_highlight_class!(pygments) if format == :html File.open(File.join(path, "pygments.#{extension}"), 'w') do |f| f.write(pygments) end end |