Safe filenames

This commit is contained in:
Nemo 2020-03-29 19:34:51 +05:30
parent 11194d648b
commit d3a603a209
5 changed files with 32 additions and 15 deletions

View File

@ -7,15 +7,12 @@ TODO
## Usage
```
muse-dl --help
USAGE: muse-dl [FLAGS] URL
FLAGS:
--no-bookmarks: Don't add bookmarks
--no-cleanup: Don't cleanup temporary files
--tmp-dir /path: Use /path as temporary directory
--output file.pdf: Use file.pdf as output filename
Usage: muse-dl [--flags] URL
--no-cleanup Don't cleanup temporary files
--tmp-dir PATH Temporary Directory to use
--output FILE Output Filename
--no-bookmarks Don't add bookmarks in the PDF
-h, --help Show this help
```
## License

View File

@ -3,22 +3,27 @@ require "./pdftk.cr"
require "./fetch.cr"
require "./book.cr"
require "./journal.cr"
require "./util.cr"
# TODO: Write documentation for `Muse::Dl`
module Muse::Dl
VERSION = "0.1.0"
# TODO: Put your code here
class Main
def self.run(args : Array(String))
parser = Parser.new(args)
thing = Fetch.get_info(parser.url)
if thing.is_a? Muse::Dl::Book
# Will have no effect if parser has a custom title
parser.output = Util.slug_filename "#{thing.title}.pdf"
# Save each chapter
thing.chapters.each do |chapter|
Fetch.save_chapter(parser.tmp, chapter[0], chapter[1], parser.bookmarks)
end
chapter_ids = thing.chapters.map { |c| c[0] }
# Stitch the PDFs together
pdf_builder = Pdftk.new(parser.tmp)
temp_stitched_file = pdf_builder.stitch chapter_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)

View File

@ -6,11 +6,18 @@ module Muse::Dl
@bookmarks = true
@tmp : String
@cleanup = true
@output = "tempfilename.pdf"
@output = DEFAULT_FILE_NAME
@url = "INVALID_URL"
DEFAULT_FILE_NAME = "tempfilename.pdf"
getter :bookmarks, :tmp, :cleanup, :output, :url
# Update the output filename unless we have a custom one passed
def output=(output_file : String)
@output = output_file unless @output != DEFAULT_FILE_NAME
end
def find_next(arg : Array(String), flag : String, default)
search = arg.index flag
if search

View File

@ -61,19 +61,19 @@ module Muse::Dl
InfoValue: Project MUSE (https://muse.jhu.edu/)
InfoBegin
InfoKey: Producer
InfoValue: Muse-DL
InfoValue: Muse-DL/#{Muse::Dl::VERSION}
InfoBegin
InfoKey: Title
InfoValue: #{book.title}
InfoBegin
InfoKey: Keywords
InfoValue: Publisher: #{book.publisher}, Published #{book.date}
InfoValue: Publisher:#{book.publisher}, Published:#{book.date}
InfoBegin
InfoKey: Author
InfoValue: #{book.author}
InfoBegin
InfoKey: Subject
InfoValue: #{book.summary.gsub("\n", " ")}
InfoValue: #{book.summary.gsub(/\n\s+/, " ")}
EOT
# Known Info keys, if they are present

8
src/util.cr Normal file
View File

@ -0,0 +1,8 @@
module Muse::Dl
class Util
# Generates a safe filename
def self.slug_filename(input : String)
input.strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
end
end
end