Rakefile 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. task default: %w(develop)
  2. begin
  3. require 'colorize'
  4. String.disable_colorization = false
  5. rescue LoadError
  6. end
  7. require 'set'
  8. STORY_DIR=File.join(ENV['USERPROFILE'], 'Documents\\Twine\\Stories')
  9. STORY_TITLE = 'Volleyball'
  10. STORY_FORMAT = 'SugarCube'
  11. STORY_FORMAT_PATH = "story_formats/sugarcube-2.28.2-for-twine-2.1-local/sugarcube-2"
  12. STORY_FORMAT_VERSION = '2.28.2'
  13. STORY_RELEASE_TITLE = 'Volleyball Chapter 1 v0.5'
  14. FILES = {
  15. '01_intro' => '01 Intro',
  16. '02_transform' => '02 Transform',
  17. '03_custom_girl' => '03 Custom Girl',
  18. '04_money_game' => '04 Money Games',
  19. '05_beaches' => '05 Beaches',
  20. '06_public_beach' => '06 Public Beach',
  21. }
  22. def files
  23. FILES.map do |short_name, long_name|
  24. OpenStruct.new({
  25. short_name: short_name,
  26. long_name: long_name,
  27. source_file: "story/#{short_name}.tw2",
  28. dest_file: File.join(STORY_DIR, "#{STORY_TITLE} - #{long_name}.html"),
  29. wrapper_file: "#{short_name}_wrapper.tw2",
  30. })
  31. end
  32. end
  33. task develop: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  34. sh "bundle exec twee2 build '--format=#{STORY_FORMAT_PATH}' main.tw2 develop.html"
  35. end
  36. task watch: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  37. sh 'start watch.html' # FIXME non-windows
  38. sh "bundle exec twee2 watch '--format=#{STORY_FORMAT_PATH}' main.tw2 watch.html"
  39. end
  40. task release: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  41. sh "bundle exec twee2 build '--format=#{STORY_FORMAT_PATH}' main.tw2 release.html"
  42. end
  43. task :storyjs do
  44. File.write(
  45. 'storyjs.tw2',
  46. File.read('storyjs.tw2')\
  47. .sub(/\$STORY_FORMAT_VERSION = '[^']*'/, "$STORY_FORMAT_VERSION = '#{STORY_FORMAT_VERSION}'")
  48. )
  49. end
  50. file 'story.tw2' do
  51. File.open('story.tw2', 'w') do |f|
  52. f.puts('::StoryIncludes')
  53. files.each do |file|
  54. f.puts(file.source_file)
  55. end
  56. end
  57. end
  58. file 'story_title.tw2' do
  59. File.open('story_title.tw2', 'w') do |f|
  60. f.puts('::StoryTitle')
  61. f.puts("#{STORY_RELEASE_TITLE}")
  62. end
  63. end
  64. desc 'export sub files to Twine 2'
  65. task export: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  66. files.each do |file|
  67. IO.write file.wrapper_file, <<~EOF
  68. ::StoryTitle
  69. Volleyball - #{file.long_name}
  70. ::Twee2Settings [twee2]
  71. @story_start_name = 'Start #{file.long_name}'
  72. Twee2::build_config.story_format = '#{STORY_FORMAT}'
  73. Twee2::build_config.story_format_version = '#{STORY_FORMAT_VERSION}'
  74. Twee2::build_config.story_ifid = ''
  75. ::StoryIncludes
  76. images.tw2
  77. stylesheet.tw2
  78. storyjs.tw2
  79. #{file.source_file}
  80. EOF
  81. sh(
  82. 'bundle', 'exec', 'twee2', 'export',
  83. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  84. file.wrapper_file, file.dest_file,
  85. )
  86. end
  87. sh(
  88. 'bundle', 'exec', 'twee2', 'export',
  89. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  90. 'main.tw2', STORY_DIR,
  91. )
  92. end
  93. desc 'import sub files from Twine 2'
  94. task import: %w(bundle_install) do
  95. files.each do |file|
  96. sh(
  97. 'bundle', 'exec', 'twee2', 'decompile',
  98. file.dest_file, file.source_file,
  99. '--exclude=StoryJS,StoryCSS,StoryTitle,Twee2Settings',
  100. '--exclude-from=../stylesheet.tw2,../storyjs.tw2,../stylesheet.tw2,../images.tw2',
  101. )
  102. end
  103. end
  104. task import_main: %w(bundle_install) do
  105. file_set = Set.new(files)
  106. base_excludes=%w[../stylesheet.tw2 ../storyjs.tw2 ../stylesheet.tw2 ../images.tw2]
  107. files.each do |file|
  108. other_files = (file_set - Set.new([file])).map { |f| File.basename(f.source_file) }
  109. excludes = other_files + base_excludes
  110. sh(
  111. 'bundle', 'exec', 'twee2', 'decompile',
  112. File.join(STORY_DIR, STORY_RELEASE_TITLE + '.html'),
  113. './' + file.source_file,
  114. '--exclude=StoryJS,StoryCSS,StoryTitle,Twee2Settings',
  115. "--exclude-from=#{excludes.join(',')}",
  116. )
  117. end
  118. end
  119. task :bundle_install do
  120. sh 'bundle check || bundle install'
  121. end
  122. task :clean do
  123. # TODO
  124. end
  125. task :autofix do
  126. #check links
  127. files.each do |file|
  128. twee_source = File.read(file.source_file)
  129. twee_source = twee_source.lines.each_with_index.map do |line, lineno|
  130. line.chomp!
  131. line.gsub(%r{\[\[(?<link>[^\]]*?)\](?<link_mods>\[[^\]]*\])?\]}) do |m|
  132. link = $~[:link]
  133. link_mods = $~[:link_mods]
  134. case link.scan('->').length
  135. when 0
  136. # this style of link doesn't need to be quoted, but it also shouldn't contain special chars
  137. "[[#{link}]#{link_mods}]"
  138. when 1
  139. text, dest = link.split('->')
  140. quote_text = true
  141. if link.start_with?('"') && text.include?("\\")
  142. text = text.end_with?('"') ? text[1..-2] : text[1..-1]
  143. elsif text.start_with?("'")
  144. text = text.end_with?("'") ? text[1..-2] : text[1..-1]
  145. elsif text =~ /\\|"/
  146. quote_text = true
  147. else
  148. quote_text = false
  149. end
  150. if quote_text
  151. text.gsub!(/(?<!\\)'/) { "\\'" }
  152. text.gsub!(/\\"/) { '"' }
  153. text.gsub!(/(?<!\\)\\(?!['\\])/) { "\\\\" }
  154. text = "'#{text}'"
  155. #FIXME This assumption seems dangerous?
  156. # if begins with a double quote ensures it ends with one too.
  157. if text[1] == '"' && text[-2] != '"'
  158. text = text[0..-3] + "\"'"
  159. end
  160. end
  161. "[[#{text}->#{dest}]#{link_mods}]"
  162. else
  163. # TODO this doesn't really happen very much in practice, so don't worry about an autofix, just passthru
  164. warn('multiple -> in link')
  165. "[[#{link}]#{link_mods}]"
  166. end
  167. end
  168. end.join("\n") + "\n"
  169. File.write(file.source_file, twee_source)
  170. end
  171. end
  172. task :lint do
  173. titles = Set.new
  174. passages = {}
  175. title = nil
  176. files.each do |file|
  177. twee_source = IO.read(file.source_file)
  178. twee_source.lines.each_with_index do |line, lineno|
  179. lineno += 1
  180. # check for duplicate
  181. if line.start_with?('::') && !line.start_with?('::StoryIncludes')
  182. title = line.match(/^:: *([^\[]*?) *(\[(.*?)\])? *(<(.*?)>)? *$/)[1]
  183. #puts title
  184. if title != 'Twee2Settings' && titles.include?(title)
  185. puts "#{file.source_file}:#{lineno} duplicate title #{title}".red
  186. end
  187. titles.add(title)
  188. passages[title] = {outbound_links: [], inbound_links: [], source_line: "#{file.source_file}:#{lineno}"}
  189. end
  190. # check links
  191. line
  192. .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
  193. .map { |x| x[0] }
  194. .each do |link|
  195. case link.scan('->').length
  196. when 0
  197. #link contains no ->, probably not a problem
  198. text = dest = link
  199. passages[title][:outbound_links] << \
  200. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  201. when 1
  202. # no problem
  203. text, dest = link.split('->')
  204. passages[title][:outbound_links] << \
  205. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  206. else
  207. puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
  208. next
  209. end
  210. if text.start_with?('\'')
  211. if !text.end_with?('\'')
  212. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
  213. next
  214. end
  215. text = text[1..-2]
  216. text
  217. .to_enum(:scan, '\'')
  218. .map {Regexp.last_match.offset(0)[0]}
  219. .each do |offset|
  220. if text[offset - 1] != '\\'
  221. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
  222. end
  223. end
  224. elsif text.start_with?('"')
  225. if !text.end_with?('"')
  226. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
  227. next
  228. end
  229. text = text[1..-2]
  230. text
  231. .to_enum(:scan, '"')
  232. .map {Regexp.last_match.offset(0)[0]}
  233. .each do |offset|
  234. if text[offset - 1] != '\\'
  235. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
  236. end
  237. end
  238. elsif text =~ /\A[\[\]'"\\]*\z/
  239. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
  240. end
  241. end
  242. # check for default text
  243. if line.strip == 'Double-click this passage to edit it.'
  244. puts "#{file.source_file}:#{lineno} has the default text \"Double-click this passage to edit it.\""
  245. end
  246. # check for TODO/FIXME
  247. if line.include?('TODO') || line.include?('FIXME')
  248. puts "#{file.source_file}:#{lineno} has TODO/FIXME text: #{line}"
  249. end
  250. # check for <<include>> as string
  251. if line.include?('<<include') && !line.include?('[[')
  252. puts "#{file.source_file}:#{lineno} has an <<include>>, but no link"
  253. end
  254. end
  255. end
  256. passages.each do |title, passage|
  257. passage[:outbound_links].each do |link|
  258. if passages[link[:passage]]
  259. passages[link[:passage]][:inbound_links] << {passage: title, source_file: passage[:source_line]}
  260. else
  261. puts "#{link[:source_line]} (title=#{title}) links to non-existent passage #{link[:passage]}".red
  262. end
  263. end
  264. end
  265. passages.each do |title, passage|
  266. if passage[:inbound_links].empty? && title != 'Twee2Settings'
  267. puts "#{passage[:source_line]} (title=#{title}) has no inbound links"
  268. end
  269. end
  270. end