rmd_reader.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #-*- conding: utf-8 -*-
  2. import os
  3. import warnings
  4. import logging
  5. logger = logging.getLogger('RMD_READER')
  6. from pelican import readers
  7. from pelican import signals
  8. from pelican import settings
  9. knitr = None
  10. rmd = False
  11. fig_path = None
  12. def initsignal(pelicanobj):
  13. global knitr, rmd, fig_path, robjects
  14. try:
  15. with warnings.catch_warnings():
  16. warnings.simplefilter("ignore")
  17. from rpy2.robjects.packages import importr
  18. import rpy2.robjects as robjects
  19. knitr = importr('knitr')
  20. idx = knitr.opts_knit.names.index('set')
  21. PATH = pelicanobj.settings.get('PATH','%s/content' % settings.DEFAULT_CONFIG.get('PATH'))
  22. logger.debug("RMD_READER PATH = %s", PATH)
  23. knitr.opts_knit[idx](**{'base.dir': PATH})
  24. knitroptsknit = pelicanobj.settings.get('RMD_READER_KNITR_OPTS_KNIT', None)
  25. if knitroptsknit:
  26. knitr.opts_knit[idx](**{str(k): v for k,v in knitroptsknit.items()})
  27. idx = knitr.opts_chunk.names.index('set')
  28. knitroptschunk = pelicanobj.settings.get('RMD_READER_KNITR_OPTS_CHUNK', None)
  29. if knitroptschunk:
  30. fig_path = knitroptschunk['fig.path'] if 'fig.path' in knitroptschunk else 'figure/'
  31. knitr.opts_chunk[idx](**{str(k): v for k,v in knitroptschunk.items()})
  32. rmd = True
  33. except ImportError as ex:
  34. rmd = False
  35. class RmdReader(readers.BaseReader):
  36. file_extensions = ['Rmd', 'rmd']
  37. @property
  38. def enabled():
  39. return rmd
  40. # You need to have a read method, which takes a filename and returns
  41. # some content and the associated metadata.
  42. def read(self, filename):
  43. """Parse content and metadata of markdown files"""
  44. QUIET = self.settings.get('RMD_READER_KNITR_QUIET', True)
  45. ENCODING = self.settings.get('RMD_READER_KNITR_ENCODING', 'UTF-8')
  46. CLEANUP = self.settings.get('RMD_READER_CLEANUP', True)
  47. RENAME_PLOT = self.settings.get('RMD_READER_RENAME_PLOT', 'chunklabel')
  48. if type(RENAME_PLOT) is bool:
  49. logger.error("RMD_READER_RENAME_PLOT takes a string value (either chunklabel or directory), please see the readme.")
  50. if RENAME_PLOT:
  51. RENAME_PLOT = 'chunklabel'
  52. logger.error("Defaulting to chunklabel")
  53. else:
  54. RENAME_PLOT = 'disabled'
  55. logger.error("Disabling plot renaming")
  56. logger.debug("RMD_READER_KNITR_QUIET = %s", QUIET)
  57. logger.debug("RMD_READER_KNITR_ENCODING = %s", ENCODING)
  58. logger.debug("RMD_READER_CLEANUP = %s", CLEANUP)
  59. logger.debug("RMD_READER_RENAME_PLOT = %s", RENAME_PLOT)
  60. # replace single backslashes with double backslashes
  61. filename = filename.replace('\\', '\\\\')
  62. # parse Rmd file - generate md file
  63. md_filename = filename.replace('.Rmd', '.aux').replace('.rmd', '.aux')
  64. if RENAME_PLOT == 'chunklabel' or RENAME_PLOT == 'directory':
  65. if RENAME_PLOT == 'chunklabel':
  66. chunk_label = os.path.splitext(os.path.basename(filename))[0]
  67. logger.debug('Chunk label: %s', chunk_label)
  68. elif RENAME_PLOT == 'directory':
  69. chunk_label = 'unnamed-chunk'
  70. PATH = self.settings.get('PATH','%s/content' % settings.DEFAULT_CONFIG.get('PATH'))
  71. src_name = os.path.splitext(os.path.relpath(filename, PATH))[0]
  72. idx = knitr.opts_chunk.names.index('set')
  73. knitroptschunk = { 'fig.path': '%s-' % os.path.join(fig_path, src_name) }
  74. knitr.opts_chunk[idx](**{str(k): v for k,v in knitroptschunk.items()})
  75. logger.debug('Figures path: %s, chunk label: %s', knitroptschunk['fig.path'], chunk_label)
  76. robjects.r('''
  77. opts_knit$set(unnamed.chunk.label="{unnamed_chunk_label}")
  78. render_markdown()
  79. hook_plot <- knit_hooks$get('plot')
  80. knit_hooks$set(plot=function(x, options) hook_plot(paste0("{{filename}}/", x), options))
  81. '''.format(unnamed_chunk_label=chunk_label))
  82. knitr.knit(filename, md_filename, quiet=QUIET, encoding=ENCODING)
  83. # read md file - create a MarkdownReader
  84. md_reader = readers.MarkdownReader(self.settings)
  85. content, metadata = md_reader.read(md_filename)
  86. # remove md file
  87. if CLEANUP:
  88. os.remove(md_filename)
  89. return content, metadata
  90. def add_reader(readers):
  91. readers.reader_classes['rmd'] = RmdReader
  92. def register():
  93. signals.readers_init.connect(add_reader)
  94. signals.initialized.connect(initsignal)