|
| 1 | +# Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt> |
| 2 | +# |
| 3 | +# This file is part of cppreference-doc |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + |
| 18 | +from premailer import transform |
| 19 | +import cssutils |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import warnings |
| 23 | +import io |
| 24 | + |
| 25 | +def preprocess_html_merge_css(src_path, dst_path): |
| 26 | + log = logging.Logger('ignore') |
| 27 | + output = io.StringIO() |
| 28 | + handler = logging.StreamHandler(stream=output) |
| 29 | + formatter = logging.Formatter('%(levelname)s, %(message)s') |
| 30 | + handler.setFormatter(formatter) |
| 31 | + log.addHandler(handler) |
| 32 | + cssutils.log.setLog(log) |
| 33 | + |
| 34 | + with open(src_path, 'r') as a_file: |
| 35 | + with warnings.catch_warnings(): |
| 36 | + warnings.simplefilter("ignore") |
| 37 | + content = transform(a_file.read(), base_url=src_path) |
| 38 | + head = os.path.dirname(dst_path) |
| 39 | + os.makedirs(head, exist_ok=True) |
| 40 | + f = open(dst_path,"w") |
| 41 | + f.write(content) |
| 42 | + |
| 43 | + return output.getvalue() |
0 commit comments