custom_js.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python3
  2. # pelican-js: embed custom JavaScript easily
  3. # Copyright (C) 2017 Jorge Maldonado Ventura
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. """
  15. Embed JavaScript files for Pelican
  16. ==================================
  17. This plugin allows you to easily embed JavaScript files in the header (<head>)
  18. or in the body (<body>) of individual articles or pages. The JavaScript files
  19. are embedded using the <script> tag.
  20. """
  21. import os
  22. import re
  23. import shutil
  24. from pelican import signals
  25. def format_js(gen, metastring, formatter):
  26. """
  27. Create a list of URL-formatted script tags
  28. Parameters
  29. ----------
  30. gen: generator
  31. Pelican Generator
  32. metastring: string
  33. metadata['js']
  34. formatter: string
  35. String format for output.
  36. Output
  37. ------
  38. List of formatted strings
  39. """
  40. metalist = metastring.replace(' ', '').split(',')
  41. site_url = '%s'
  42. position_regex = re.compile('(\(\w+\)$)')
  43. formatted_strings = []
  44. for i in range(len(metalist)):
  45. pos = position_regex.search(metalist[i]).group()
  46. format_string = formatter.format(
  47. site_url, metalist[i][:-len(pos)], pos)
  48. formatted_strings.append(format_string)
  49. return formatted_strings
  50. def copy_resources(src, dest, file_list):
  51. """
  52. Copy files from content folder to output folder
  53. Parameters
  54. ----------
  55. src: string
  56. Content folder path
  57. dest: string,
  58. Output folder path
  59. file_list: list
  60. List of files to be transferred
  61. Output
  62. ------
  63. Copies files from content to output
  64. """
  65. if not os.path.exists(dest):
  66. os.makedirs(dest)
  67. for file_ in file_list:
  68. file_src = os.path.join(src, file_)
  69. shutil.copy2(file_src, dest)
  70. def add_tags(gen, metadata):
  71. """
  72. It will add the JS to the article or page
  73. """
  74. def replace_last(source_string, replace_what, replace_with):
  75. head, sep, tail = source_string.rpartition(replace_what)
  76. return head + replace_with + tail
  77. if 'js' in metadata.keys():
  78. minification_string = '.min'
  79. metadata['js'] = replace_last(
  80. metadata['js'],
  81. '.js',
  82. minification_string + '.js')
  83. script = '<script src="{0}/vendor/{1}"></script>{2}'
  84. metadata['js'] = format_js(gen, metadata['js'], script)
  85. def move_resources(gen):
  86. """
  87. Move JS files from js folder to output folder
  88. """
  89. js_files = gen.get_files('js', extensions='js')
  90. js_dest = os.path.join(gen.output_path, 'vendor')
  91. copy_resources(gen.path, js_dest, js_files)
  92. def register():
  93. """
  94. Plugin registration
  95. """
  96. signals.article_generator_context.connect(add_tags)
  97. signals.page_generator_context.connect(add_tags)
  98. signals.article_generator_finalized.connect(move_resources)