1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   6 from ebooklib import epub
 
   8 from librarian.dcparser import Person
 
   9 from librarian import get_resource
 
  12 def _register_function(f):
 
  13     """ Register extension function with lxml """
 
  14     ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
 
  18 def reg_substitute_entities():
 
  19     entity_substitutions = [
 
  27     def substitute_entities(context, text):
 
  28         """XPath extension function converting all entites in passed text."""
 
  29         if isinstance(text, list):
 
  31         for entity, substitutution in entity_substitutions:
 
  32             text = text.replace(entity, substitutution)
 
  35     _register_function(substitute_entities)
 
  39     def strip(context, text):
 
  40         """Remove unneeded whitespace from beginning and end"""
 
  41         if isinstance(text, list):
 
  43         return re.sub(r'\s+', ' ', text).strip()
 
  44     _register_function(strip)
 
  47 def reg_starts_white():
 
  48     def starts_white(context, text):
 
  49         if isinstance(text, list):
 
  53         return text[0].isspace()
 
  54     _register_function(starts_white)
 
  58     def ends_white(context, text):
 
  59         if isinstance(text, list):
 
  63         return text[-1].isspace()
 
  64     _register_function(ends_white)
 
  68     def wrap_words(context, text, wrapping):
 
  70         XPath extension function automatically wrapping words
 
  73         if isinstance(text, list):
 
  78         words = re.split(r'\s', text)
 
  83             line_length += len(word) + 1
 
  84             if line_length > wrapping:
 
  85                 # Max line length was exceeded. We create new line
 
  87                 line_length = len(word)
 
  88             lines[-1].append(word)
 
  89         return '\n'.join(' '.join(line) for line in lines)
 
  90     _register_function(wrap_words)
 
  93 def reg_person_name():
 
  94     def person_name(context, text):
 
  95         """ Converts "Name, Forename" to "Forename Name" """
 
  96         if isinstance(text, list):
 
  98         return Person.from_text(text).readable()
 
  99     _register_function(person_name)
 
 102 def reg_texcommand():
 
 103     def texcommand(context, text):
 
 104         """Remove non-letters"""
 
 105         if isinstance(text, list):
 
 107         return re.sub(r'[^a-zA-Z]', '', text).strip()
 
 108     _register_function(texcommand)
 
 111 def lang_code_3to2(text):
 
 112     """Convert 3-letter language code to 2-letter code"""
 
 115     with open(get_resource('res/ISO-639-2_8859-1.txt'), 'rb') as f:
 
 116         for line in f.read().decode('latin1').split('\n'):
 
 117             codes = line.strip().split('|')
 
 126 def mathml_latex(context, trees):
 
 127     from librarian.embeds.mathml import MathML
 
 128     text = MathML(trees[0]).to_latex().data
 
 129     # Remove invisible multiplications, they produce unwanted spaces.
 
 130     text = text.replace('\u2062', '')
 
 134 def reg_mathml_latex():
 
 135     _register_function(mathml_latex)
 
 138 def reg_mathml_epub(output):
 
 139     from librarian.embeds.mathml import MathML
 
 141     def mathml(context, trees):
 
 142         data = MathML(trees[0]).to_latex().to_png().data
 
 143         name = "math%d.png" % mathml.count
 
 147                 uid='math%d' % mathml.count,
 
 149                 media_type='image/png',
 
 156     _register_function(mathml)