custom_css.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. # pelican-css: embed custom CSS 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 CSS files for Pelican
  16. ===========================
  17. This plugin allows you to easily embed CSS files in the header of individual
  18. articles or pages. The CSS files are embedded using the HTML <link> tag
  19. inside the <head> tag.
  20. """
  21. import os
  22. import shutil
  23. from pelican import signals
  24. def format_css(gen, metastring, formatter):
  25. """
  26. Create a list of URL-formatted style tags
  27. Parameters
  28. ----------
  29. gen: generator
  30. Pelican Generator
  31. metastring: string
  32. metadata['scripts'] or metadata['styles']
  33. formatter: string
  34. String format for output.
  35. Output
  36. ------
  37. List of formatted strings
  38. """
  39. metalist = metastring.replace(' ', '').split(',')
  40. site_url = '%s'
  41. return [formatter.format(site_url, x) for x in metalist]
  42. def copy_resources(src, dest, file_list):
  43. """
  44. Copy files from content folder to output folder
  45. Parameters
  46. ----------
  47. src: string
  48. Content folder path
  49. dest: string,
  50. Output folder path
  51. file_list: list
  52. List of files to be transferred
  53. Output
  54. ------
  55. Copies files from content to output
  56. """
  57. if not os.path.exists(dest):
  58. os.makedirs(dest)
  59. for file_ in file_list:
  60. file_src = os.path.join(src, file_)
  61. shutil.copy2(file_src, dest)
  62. def add_tags(gen, metadata):
  63. """
  64. It will add the CSS to the article
  65. """
  66. if 'css' in metadata.keys():
  67. style = '<link rel="stylesheet" href="{0}/css/{1}" type="text/css">'
  68. metadata['styles'] = format_css(gen, metadata['css'], style)
  69. def move_resources(gen):
  70. """
  71. Move CSS files from css folder to output folder
  72. """
  73. css_files = gen.get_files('css', extensions='css')
  74. css_dest = os.path.join(gen.output_path, 'css')
  75. copy_resources(gen.path, css_dest, css_files)
  76. def register():
  77. """
  78. Plugin registration
  79. """
  80. signals.article_generator_context.connect(add_tags)
  81. signals.page_generator_context.connect(add_tags)
  82. signals.article_generator_finalized.connect(move_resources)