Class: Ffnpdf::Story

Inherits:
Object
  • Object
show all
Defined in:
lib/ffnpdf/story.rb

Constant Summary collapse

FFN_URL =
"http://www.fanfiction.net/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(story_id) ⇒ Story

Returns a new instance of Story.



11
12
13
# File 'lib/ffnpdf/story.rb', line 11

def initialize(story_id)
  @story_id = story_id
end

Instance Attribute Details

#custom_urlObject

Returns the value of attribute custom_url.



9
10
11
# File 'lib/ffnpdf/story.rb', line 9

def custom_url
  @custom_url
end

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/ffnpdf/story.rb', line 9

def error
  @error
end

Instance Method Details

#build_storyObject



166
167
168
169
170
# File 'lib/ffnpdf/story.rb', line 166

def build_story
  return unless check_story_dir
  combine_mds
  convert_to_pdf
end

#chapter_mdsObject



56
57
58
# File 'lib/ffnpdf/story.rb', line 56

def chapter_mds
  Dir["#{@story_id}/*"].grep(/.md$/).reject { |x| /combined.md$/.match x }.sort
end

#check_storyObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ffnpdf/story.rb', line 23

def check_story
  return false unless check_story_id

  test_pull = HTTParty.get(story_url)
  unless test_pull.code == 200
    @error = "Story does not exist (#{story_url})"
    return false
  end

  unless Nokogiri::HTML(test_pull.body).css(".storytext")
    @error = "Story does not exist (#{story_url})"
    return false
  end

  true
end

#check_story_dirObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ffnpdf/story.rb', line 40

def check_story_dir
  return false unless check_story_id
  
  unless File.directory?(@story_id)
    @error = "Story folder does not exist (#{@story_id}/)"
    return false
  end

  unless chapter_mds.count > 0
    @error = "Story folder does not have markdown files (#{@story_id}/)"
    return false
  end

  true
end

#check_story_idObject



15
16
17
18
19
20
21
# File 'lib/ffnpdf/story.rb', line 15

def check_story_id
  unless /^[\d]*$/.match @story_id
    @error = "Story ID is invalid"
    return false
  end
  true
end

#combine_mdsObject



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ffnpdf/story.rb', line 172

def combine_mds
  puts "combining chapters"
  combined = File.new("#{@story_id}/combined.md", "w")
  chapter_mds.each do |chapter|
    IO.foreach("#{chapter}") do |line|
      combined.puts(line)
    end
    combined.puts("\\clearpage")
    combined.puts()
  end
  combined.close
end

#convert_to_pdfObject



185
186
187
# File 'lib/ffnpdf/story.rb', line 185

def convert_to_pdf
  Converter.convert_to_pdf(@story_id)
end

#generate_template(title, author, date) ⇒ Object



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
# File 'lib/ffnpdf/story.rb', line 102

def generate_template(title, author, date)
  template = File.new("xetex.template", "w")
  contents = <<-CONTENTS
% Shamelessly ripped off from https://github.com/karlseguin/the-little-mongodb-book
\\documentclass[$fontsize$,$columns$]{book}
\\usepackage{fullpage}
\\usepackage{fontspec,xltxtra,xunicode}
\\defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\\setmainfont{$mainfont$}
\\setsansfont{$sansfont$}
\\setmonofont{$monofont$}

\\setlength{\\parindent}{0pt}
\\setlength{\\parskip}{12pt plus 2pt minus 1pt}
\\linespread{1.2}

\\usepackage{listings}
\\usepackage[dvipsnames,usenames]{xcolor}

$if(fancy-enums)$
\\usepackage{enumerate}
$endif$
\\setcounter{secnumdepth}{-1}

\\usepackage{hyperref}
\\hypersetup{
colorlinks=true,%
citecolor=Black,%
filecolor=Black,%
linkcolor=Black,%
urlcolor=Black
}

\\title{#{title}}
\\author{#{author}}
\\date{#{date}}
\\begin{document}
$body$
\\end{document}
  CONTENTS
  template.puts(contents)
  template.close
end

#generate_title_page(has_toc = true) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ffnpdf/story.rb', line 152

def generate_title_page(has_toc = true)
  title_page = File.new("0000.md", "w")
  title = "\\maketitle\n"
  if has_toc
    title += <<-CONTENTS
\\pagenumbering{roman}
\\tableofcontents

    CONTENTS
  end
  title_page.puts(title)
  title_page.close
end

#generate_variables_fileObject



146
147
148
149
150
# File 'lib/ffnpdf/story.rb', line 146

def generate_variables_file
  variables = File.new("variables.txt", "w")
  variables.puts("-V paper=a4paper -V hmargin=3cm -V vmargin=3cm -V mainfont=\"DejaVu Serif\" -V sansfont=\"DejaVu Sans\" -V monofont=\"DejaVu Sans Mono\" -V geometry=portrait -V columns=onecolumn -V fontsize=11pt ")
  variables.close
end

#get_doc_date(doc) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/ffnpdf/story.rb', line 91

def get_doc_date(doc)
  parsing = doc.xpath('//div[@style="color:gray;"]')[0].text
  match = /Updated: (.{8})/.match parsing
  if match
    return Date.strptime(match[1], "%m-%d-%y").strftime("%d %B %Y")
  else
    match = /Published: (.{8})/.match parsing
    return Date.strptime(match[1], "%m-%d-%y").strftime("%d %B %Y")
  end
end

#pull_storyObject



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
# File 'lib/ffnpdf/story.rb', line 64

def pull_story
  return unless check_story
  FileUtils.mkdir_p @story_id
  Dir.chdir @story_id
  
  chapter1 = Chapter.new(story_url, 1)
  chapter1.pull_and_convert

  doc = chapter1.doc

  title = doc.xpath("//div/table//b")[0].text
  author = doc.xpath("//div/table//a[not(@title or @onclick)]")[0].text

  generate_template(title, author, get_doc_date(doc))
  generate_variables_file
  generate_title_page(chapter1.chapter_title)

  chapters = doc.xpath('//form//select[@name="chapter"]/option').count

  if chapters > 1
    (2..chapters).each do |chapter| 
      Chapter.new("#{story_url}#{chapter}/", chapter).pull_and_convert
    end
  end
  Dir.chdir "../" 
end

#story_urlObject



60
61
62
# File 'lib/ffnpdf/story.rb', line 60

def story_url
  custom_url ? "#{@custom_url}s/#{@story_id}/" : "#{FFN_URL}s/#{@story_id}/"
end