UnityGameDirective.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2017 Mr.Page
  3. # Permission is hereby granted, free of charge, to any person obtaininga copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in
  10. # all copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. from __future__ import unicode_literals
  19. from docutils import nodes
  20. from docutils.parsers.rst import directives, Directive
  21. from . import config
  22. import os
  23. class UnityWebgl(Directive):
  24. required_arguments = 1
  25. optional_arguments = 4
  26. option_spec = {
  27. 'width': directives.positive_int,
  28. 'height': directives.positive_int,
  29. 'gameroot': directives.unchanged_required,
  30. 'template': directives.unchanged_required,
  31. }
  32. final_argument_whitespace = False
  33. has_content = False
  34. def load_template(self, game, gamesroot, templatepath, width, height):
  35. basepath = os.path.dirname(__file__)
  36. filepath = os.path.abspath(os.path.join(basepath, 'template.txt'))
  37. with open(filepath, 'r') as template:
  38. data = template.read()
  39. return data.format(game, gamesroot, templatepath, width, height)
  40. def run(self):
  41. # load config
  42. game = self.arguments[0].strip()
  43. games_path = config.GAMES_ROOT_DIR
  44. template_path = config.TEMPLATE_PATH
  45. width = config.DEFAULT_WIDTH
  46. height = config.DEFAULT_HEIGHT
  47. # get params
  48. if 'width' in self.options:
  49. width = self.options['width']
  50. if 'height' in self.options:
  51. height = self.options['height']
  52. if 'gameroot' in self.options:
  53. games_path = self.options['gameroot']
  54. if 'template' in self.options:
  55. template_path = self.options['template']
  56. # remove slashes
  57. games_path = games_path.rstrip('/')
  58. template_path = template_path.rstrip('/')
  59. html = self.load_template(game, games_path, template_path, width, height)
  60. return [
  61. nodes.raw('', html, format='html')]
  62. def register():
  63. directives.register_directive('unitywebgl', UnityWebgl)