<commit_before>import os
from flask import Flask
from flask import request
from flask import abort
import json

# use the pygments library for code highlighting
from pygments import highlight
from pygments.lexers import *
from pygments.formatters import HtmlFormatter

# use premailer to inline the styles from pygments for email client compatibility
from premailer import transform

app = Flask(__name__)

lexer_map = {
    'python': PythonLexer,
    'diff': DiffLexer
}

@app.route('/<lexer>', methods=['POST'])
def highlight_post(lexer):
    if not lexer in lexer_map:
        abort(404)

    post = request.form.copy()
    if post['body-html']:
        return json.dumps(post)

    highlighted_code = highlight(post['body-plain'], lexer_map[lexer](), HtmlFormatter())
    highlighted_code = "<style>{0}</style>\n{1}".format(HtmlFormatter().get_style_defs(), highlighted_code)
    post['body-html'] = transform(highlighted_code)

    highlighted_code = highlight(post['stripped-text'], lexer_map[lexer](), HtmlFormatter())
    highlighted_code = "<style>{0}</style>\n{1}".format(HtmlFormatter().get_style_defs(), highlighted_code)
    post['stripped-html'] = transform(highlighted_code)

    return json.dumps(post)

@app.route('/', methods=['GET'])
def info():
    return 'Syntax highlighting webhook processor for Threadable! See: https://github.com/otherio/threadable-pygments'<commit_msg>the body-html key is absent when there's no html part<commit_after>import os
from flask import Flask
from flask import request
from flask import abort
import json

# use the pygments library for code highlighting
from pygments import highlight
from pygments.lexers import *
from pygments.formatters import HtmlFormatter

# use premailer to inline the styles from pygments for email client compatibility
from premailer import transform

app = Flask(__name__)

lexer_map = {
    'python': PythonLexer,
    'diff': DiffLexer
}

@app.route('/<lexer>', methods=['POST'])
def highlight_post(lexer):
    if not lexer in lexer_map:
        abort(404)

    post = request.form.copy()
    if 'body-html' in post and post['body-html']:
        return json.dumps(post)

    highlighted_code = highlight(post['body-plain'], lexer_map[lexer](), HtmlFormatter())
    highlighted_code = "<style>{0}</style>\n{1}".format(HtmlFormatter().get_style_defs(), highlighted_code)
    post['body-html'] = transform(highlighted_code)

    highlighted_code = highlight(post['stripped-text'], lexer_map[lexer](), HtmlFormatter())
    highlighted_code = "<style>{0}</style>\n{1}".format(HtmlFormatter().get_style_defs(), highlighted_code)
    post['stripped-html'] = transform(highlighted_code)

    return json.dumps(post)

@app.route('/', methods=['GET'])
def info():
    return 'Syntax highlighting webhook processor for Threadable! See: https://github.com/otherio/threadable-pygments'
{'fraction_non_alphanumeric': 0.12648913805185705, 'fraction_numerical': 0.004905395935529082, 'mean_word_length': 5.10042735042735, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1243832', 'n_tokens_mistral': 898, 'n_tokens_neox': 858, 'n_words': 224}