Rakefile 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. task default: %w(develop)
  2. STORY_DIR=File.join(ENV['USERPROFILE'], 'Documents\\Twine\\Stories')
  3. STORY_TITLE = 'Volleyball'
  4. FILES = {
  5. 'intro' => 'Intro',
  6. 'transform' => 'Transform',
  7. 'custom_girl' => 'Custom Girl',
  8. 'beaches' => 'Beaches',
  9. 'money_game' => 'Money Games',
  10. }
  11. def files
  12. FILES.map do |short_name, long_name|
  13. OpenStruct.new({
  14. short_name: short_name,
  15. long_name: long_name,
  16. source_file: "#{short_name}.tw2",
  17. dest_file: File.join(STORY_DIR, "#{STORY_TITLE} - #{long_name}.html"),
  18. wrapper_file: "#{short_name}_wrapper.tw2",
  19. })
  20. end
  21. end
  22. task develop: %w(bundle_install) do
  23. sh "bundle exec twee2 build main.tw2 develop.html"
  24. end
  25. task watch: %w(bundle_install) do
  26. sh 'start watch.html' # FIXME non-windows
  27. sh "bundle exec twee2 watch main.tw2 watch.html"
  28. end
  29. task release: %w(bundle_install) do
  30. sh "bundle exec twee2 build main.tw2 release.html"
  31. end
  32. desc 'export sub files to Twine 2'
  33. task export: %w(bundle_install) do
  34. files.each do |file|
  35. IO.write file.wrapper_file, <<~EOF
  36. ::StoryTitle
  37. Volleyball - #{file.long_name}
  38. ::Twee2Settings [twee2]
  39. @story_start_name = 'Start #{file.long_name}'
  40. Twee2::build_config.story_format = 'SugarCube2'
  41. ::StoryIncludes
  42. images.tw2
  43. stylesheet.tw2
  44. storyjs.tw2
  45. #{file.source_file}
  46. EOF
  47. sh 'bundle', 'exec', 'twee2', 'export', file.wrapper_file, file.dest_file
  48. end
  49. sh 'bundle', 'exec', 'twee2', 'export', 'main.tw2', STORY_DIR
  50. end
  51. desc 'import sub files from Twine 2'
  52. task import: %w(bundle_install) do
  53. files.each do |file|
  54. sh(
  55. 'bundle', 'exec', 'twee2', 'decompile',
  56. file.dest_file, file.source_file,
  57. '--exclude=StoryJS,StoryCSS,StoryTitle,Twee2Settings',
  58. '--exclude-from=storyjs.tw2,stylesheet.tw2,images.tw2"',
  59. )
  60. end
  61. end
  62. task :bundle_install do
  63. sh 'bundle install'
  64. end
  65. task :clean do
  66. # TODO
  67. end
  68. task :lint do
  69. FILES.each do |short_name, long_name|
  70. filename = "#{short_name}.tw2"
  71. twee_source = IO.read(filename)
  72. twee_source.lines.each_with_index do |line, lineno|
  73. lineno += 1
  74. line
  75. .scan(%r{\[\[([^\]]*)\]\]})
  76. .map { |x| x[0] }
  77. .each do |link|
  78. case link.scan('->').length
  79. when 0
  80. #puts("link contains no ->, probably not a problem")
  81. next
  82. when 1
  83. # no problem
  84. else
  85. puts("link contains multiple -> definitely a problem")
  86. next
  87. end
  88. text, dest = link.split('->')
  89. if text.start_with?('\'')
  90. if !text.end_with?('\'')
  91. puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one")
  92. next
  93. end
  94. text = text[1..-2]
  95. text
  96. .to_enum(:scan, '\'')
  97. .map {Regexp.last_match.offset(0)[0]}
  98. .each do |offset|
  99. if text[offset - 1] != '\\'
  100. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body"
  101. end
  102. end
  103. elsif text.start_with?('"')
  104. if !text.end_with?('"')
  105. puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one")
  106. next
  107. end
  108. text = text[1..-2]
  109. text
  110. .to_enum(:scan, '"')
  111. .map {Regexp.last_match.offset(0)[0]}
  112. .each do |offset|
  113. if text[offset - 1] != '\\'
  114. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body"
  115. end
  116. end
  117. elsif text =~ /\A[\[\]'"\\]*\Z/
  118. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes"
  119. end
  120. #puts(dest)
  121. end
  122. end
  123. end
  124. end