loi.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. """
  3. Pandoc filter to convert divs with class="loi" to LaTeX
  4. loi environments in LaTeX output.
  5. TODO: allow this to work not only for loi but for any custom class/environment
  6. Based on John MacFarlane's theorem.py pandocfilters' example.
  7. """
  8. """
  9. Copyright (c) 2013, John MacFarlane
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. - Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. - Neither the name of John Macfarlane nor the names of its contributors may
  19. be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """
  32. from pandocfilters import toJSONFilter, RawBlock, Div
  33. def latex(x):
  34. return RawBlock('latex', x)
  35. def lois(key, value, format, meta):
  36. if key == 'Div':
  37. [[ident, classes, kvs], contents] = value
  38. if "loi" in classes:
  39. if format == "latex":
  40. if ident == "":
  41. label = ""
  42. else:
  43. label = '\\label{' + ident + '}'
  44. return([latex('\\begin{loi}' + label)] + contents +
  45. [latex('\\end{loi}')])
  46. elif format == "html" or format == "html5":
  47. newcontents = [html('<div class="loi"><blockquote> ' + contents + '</blockquote></div>')]
  48. return Div([ident, classes, kvs], newcontents)
  49. if __name__ == "__main__":
  50. toJSONFilter(lois)