From 6a216d9b3e55c845e47d316abbf98fc7b9b6948a Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Sat, 7 Aug 2021 17:50:43 -0500 Subject: [PATCH] Add a 'module' for generating a MediaWiki template invocation --- src/app.rb | 19 ++++++++++++ src/data/WikiSheet.erb | 60 +++++++++++++++++++++++++++++++++++++ src/lib/wiki.rb | 22 ++++++++++++++ src/public/js/burning.js | 16 ++++++++++ src/views/partials/main.erb | 5 ++++ 5 files changed, 122 insertions(+) create mode 100644 src/data/WikiSheet.erb create mode 100644 src/lib/wiki.rb diff --git a/src/app.rb b/src/app.rb index ae21a67..868afd9 100644 --- a/src/app.rb +++ b/src/app.rb @@ -5,6 +5,7 @@ require 'json' require_relative 'lib/cache' require_relative 'lib/pdf' require_relative 'lib/data' +require_relative 'lib/wiki' use Rack::Logger set :bind, ENV['HOST'] @@ -107,6 +108,16 @@ post '/model' do "/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.model" end +post '/wiki' do + request.body.rewind + raw = request.body.readpartial(16 * 1024) + data = JSON.parse(raw) + key = "char-#{Time.now.strftime('%Y%m%d%H%M%S%L')}-#{rand(1...10000)}" + CACHE.store key, data + + "/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.wiki" +end + get '/get_file' do data = nil if params['download_name'].match(/\.pdf$/) @@ -116,6 +127,14 @@ get '/get_file' do cs = CharSheet.new(data) data = cs.render(logger, DATA) end + elsif params['download_name'].match(/\.wiki$/) + content_type 'text/plain' + data = CACHE.delete(params['file']) + if data + wiki_sheet = WikiSheet.new(data) + p wiki_sheet + data = wiki_sheet.render + end else content_type 'application/octet-stream' data = JSON.dump CACHE.delete(params['file']) diff --git a/src/data/WikiSheet.erb b/src/data/WikiSheet.erb new file mode 100644 index 0000000..692d5d6 --- /dev/null +++ b/src/data/WikiSheet.erb @@ -0,0 +1,60 @@ +{{BWCharacterSheet +|name=<%= name %> +|stock=<%= stock.capitalize %> +|age=<%= age %> +|homeland= +|features= + +<% lifepaths.each.with_index do |lifepath, i| %> +|lifepath<%= i+1 %>=<%= lifepath %> +<% end %> + +|fate= +|persona= +|deeds= + +|belief1= +|belief2= +|belief3= +<%# |belief_special= %> + +|instinct1= +|instinct2= +|instinct3= + +|character_traits={{BWCharacterSheet/TraitList +<% for trait in traits.filter { |trait| trait[1] == "character" }%> +|<%= trait[0] %> +<% end %> +}} + +|die_traits={{BWCharacterSheet/TraitList +<% for trait in traits.filter { |trait| trait[1] == "die" }%> +|<%= trait[0] %> +<% end %> +}} + +|call-on_traits={{BWCharacterSheet/TraitList +<% for trait in traits.filter { |trait| trait[1] == "call_on" }%> +|<%= trait[0] %> +<% end %> +}} + +|will=<%= stats["will"][0] %><%= stats["will"][1] %>,0,0,0,0,0 +|perception=<%= stats["perception"][0] %><%= stats["perception"][1] %>,0,0,0,0,0 +|power=<%= stats["power"][0] %><%= stats["power"][1] %>,0,0,0,0,0 +|forte=<%= stats["forte"][0] %><%= stats["forte"][1] %>,0,0,0,0,0 +|agility=<%= stats["agility"][0] %><%= stats["agility"][1] %>,0,0,0,0,0 +|speed=<%= stats["speed"][0] %><%= stats["speed"][1] %>,0,0,0,0,0 + +|health=<%= attributes["health"][0] %><%= attributes["health"][1] %>,0,0,0,0,0,0 +|steel=<%= attributes["steel"][0] %><%= attributes["steel"][1] %>,0,0,0,0,0,0 +|circles=<%= attributes["circles"][0] %><%= attributes["circles"][1] %>,0,0,0,0,0,0 +|resources=<%= attributes["resources"][0] %><%= attributes["resources"][1] %>,0,0,0,0,0,0 +<%# |special_att1=Faith,B3,6,5,4,3,2,1 %> +<%# |special_att2=Grief,B3,6,5,4,3,2,1 %> + +<% skills.each.with_index do |skill,i| %> +|skill<%= i+1 %>=<%= skill[0] %>,<%= skill[1] %><%= skill[2] %>,0,0,0,0,0,0 +<% end %> +}} diff --git a/src/lib/wiki.rb b/src/lib/wiki.rb new file mode 100644 index 0000000..aef63ca --- /dev/null +++ b/src/lib/wiki.rb @@ -0,0 +1,22 @@ +require 'erb' + +class WikiSheet + include ERB::Util + attr_accessor :character + + def initialize(character) + @character = character + end + + def render() + load_template + ERB.new(@template, 0, '>').result_with_hash(@character) + end + + private + + def load_template + @template = File.read("data/WikiSheet.erb") + end + +end diff --git a/src/public/js/burning.js b/src/public/js/burning.js index 77253e6..5de227d 100644 --- a/src/public/js/burning.js +++ b/src/public/js/burning.js @@ -1447,6 +1447,22 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo }); } + $scope.downloadWikiSheet = function(){ + var json = angular.toJson($scope.convertCurrentCharacterToStructForCharSheet(), true); + $http.post("/wiki", json). + success(function(data,status,headers,config){ + console.log("huzzah, making wiki sheet succeeded. File URL: " + data); + var frame = document.getElementById("downloadframe"); + if ( frame ){ + frame.src = data; + } + }). + error(function(data,status,headers,config){ + console.log("boo, making wiki sheet failed: " + data); + $scope.addAlert('tools', "Generating Wiki character sheet failed: " + data); + }); + } + $scope.saveCurrentCharacterToServer = function(){ var nameWarn = "The character must have a name before it can be saved."; if( $scope.name.length == 0 ){ diff --git a/src/views/partials/main.erb b/src/views/partials/main.erb index 161b537..6da38d0 100644 --- a/src/views/partials/main.erb +++ b/src/views/partials/main.erb @@ -59,6 +59,11 @@ Download Character Model +
+ + Download Wiki Character Sheet + +