<commit_before>package org.jetbrains.plugins.ipnb;

import com.github.rjeschke.txtmark.Configuration;
import com.github.rjeschke.txtmark.Processor;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.ui.LafManager;
import com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ui.UIUtil;
import com.sun.javafx.webkit.Accessor;
import com.sun.webkit.WebPage;
import com.sun.webkit.graphics.WCSize;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
import org.markdown4j.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class IpnbJfxUtils {
  private static final Logger LOG = Logger.getInstance(IpnbJfxUtils.class);
  private static final String ourStyle = "<html><head><style>#mydiv\n{\n" +
                                         "min-width: %spx;\n" +
                                         "}</style>";
  private static final String ourBody = "</head><body><div id=\"mydiv\">";
  private static final String ourMathJaxPrefix = ourStyle +
                                                 "<script type=\"text/x-mathjax-config\">\n" +
                                                 "            MathJax.Hub.Config({\n" +
                                                 "                tex2jax: {\n" +
                                                 "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n" +
                                                 "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ],\n" +
                                                 "                    processEscapes: true,\n" +
                                                 "                    processEnvironments: true\n" +
                                                 "                },\n" +
                                                 "                displayAlign: 'center',\n" +
                                                 "                \"HTML-CSS\": {\n" +
                                                 "                    styles: {'#mydiv': {\"font-size\": %s}},\n" +
                                                 "                    preferredFont: null,\n" +
                                                 "                    linebreaks: { automatic: true }\n" +
                                                 "                }\n" +
                                                 "            });\n" +
                                                 "</script><script type=\"text/javascript\"\n" +
                                                 " src=\"" +
                                                 IpnbFileType.class.getResource("/MathJax/MathJax.js") +
                                                 "?config=TeX-AMS_HTML-full\">\n" +
                                                 " </script>" + ourBody;
  private static final String ourPrefix = ourStyle + ourBody;
  private static final String ourPostfix = "</div></body></html>";
  private static URL ourStyleUrl;

  public static JComponent createHtmlPanel(@NotNull final String source, int width) {

    final JFXPanel javafxPanel = new JFXPanel() {
      @Override
      protected void processMouseWheelEvent(MouseWheelEvent e) {
        final Container parent = getParent();
        final MouseEvent parentEvent = SwingUtilities.convertMouseEvent(this, e, parent);
        parent.dispatchEvent(parentEvent);
      }
    };
    javafxPanel.setBackground(IpnbEditorUtil.getBackground());

    Platform.runLater(() -> {
      final WebView webView = new WebView();
      webView.setOnDragDetected(event -> {
      });
      final WebEngine engine = webView.getEngine();
      initHyperlinkListener(engine);

      final boolean hasMath = source.contains("$");
      if (hasMath) {
        engine.setOnStatusChanged(event -> adjustHeight(webView, javafxPanel, source));
      }
      else {
        engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
          if (newValue == Worker.State.SUCCEEDED) {
            adjustHeight(webView, javafxPanel, source);
          }
        });
      }
      final BorderPane pane = new BorderPane(webView);
      final String prefix;
      if (hasMath) {
        prefix = String.format(ourMathJaxPrefix, width - 500, EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
      }
      else {
        prefix = String.format(ourPrefix, width - 500);
      }
      final String content = prefix + convertToHtml(source) + ourPostfix;
      engine.loadContent(content);

      final Scene scene = new Scene(pane, 0, 0);

      javafxPanel.setScene(scene);
      updateLaf(LafManager.getInstance().getCurrentLookAndFeel() instanceof DarculaLookAndFeelInfo,
                engine, javafxPanel);
    });

    return javafxPanel;
  }

  private static String convertToHtml(@NotNull String source) {
    final String result = wrapMath(source);

    final ExtDecorator decorator = new ExtDecorator();
    final Configuration.Builder builder = Configuration.builder().forceExtentedProfile()
      .registerPlugins(new Plugin[]{new YumlPlugin(), new WebSequencePlugin(), new IncludePlugin()}).setDecorator(decorator)
      .setCodeBlockEmitter(new CodeBlockEmitter());
    String processed = Processor.process(result, builder.build());
    processed = unwrapMath(processed);

    return processed;<commit_msg>quick fix for idea community tests.<commit_after>package org.jetbrains.plugins.ipnb;

//import com.github.rjeschke.txtmark.Configuration;
//import com.github.rjeschke.txtmark.Processor;

import com.intellij.ide.BrowserUtil;
import com.intellij.ide.ui.LafManager;
import com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ui.UIUtil;
import com.sun.javafx.webkit.Accessor;
import com.sun.webkit.WebPage;
import com.sun.webkit.graphics.WCSize;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

//import org.markdown4j.*;

public class IpnbJfxUtils {
  private static final Logger LOG = Logger.getInstance(IpnbJfxUtils.class);
  private static final String ourStyle = "<html><head><style>#mydiv\n{\n" +
                                         "min-width: %spx;\n" +
                                         "}</style>";
  private static final String ourBody = "</head><body><div id=\"mydiv\">";
  private static final String ourMathJaxPrefix = ourStyle +
                                                 "<script type=\"text/x-mathjax-config\">\n" +
                                                 "            MathJax.Hub.Config({\n" +
                                                 "                tex2jax: {\n" +
                                                 "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n" +
                                                 "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ],\n" +
                                                 "                    processEscapes: true,\n" +
                                                 "                    processEnvironments: true\n" +
                                                 "                },\n" +
                                                 "                displayAlign: 'center',\n" +
                                                 "                \"HTML-CSS\": {\n" +
                                                 "                    styles: {'#mydiv': {\"font-size\": %s}},\n" +
                                                 "                    preferredFont: null,\n" +
                                                 "                    linebreaks: { automatic: true }\n" +
                                                 "                }\n" +
                                                 "            });\n" +
                                                 "</script><script type=\"text/javascript\"\n" +
                                                 " src=\"" +
                                                 IpnbFileType.class.getResource("/MathJax/MathJax.js") +
                                                 "?config=TeX-AMS_HTML-full\">\n" +
                                                 " </script>" + ourBody;
  private static final String ourPrefix = ourStyle + ourBody;
  private static final String ourPostfix = "</div></body></html>";
  private static URL ourStyleUrl;

  public static JComponent createHtmlPanel(@NotNull final String source, int width) {

    final JFXPanel javafxPanel = new JFXPanel() {
      @Override
      protected void processMouseWheelEvent(MouseWheelEvent e) {
        final Container parent = getParent();
        final MouseEvent parentEvent = SwingUtilities.convertMouseEvent(this, e, parent);
        parent.dispatchEvent(parentEvent);
      }
    };
    javafxPanel.setBackground(IpnbEditorUtil.getBackground());

    Platform.runLater(() -> {
      final WebView webView = new WebView();
      webView.setOnDragDetected(event -> {
      });
      final WebEngine engine = webView.getEngine();
      initHyperlinkListener(engine);

      final boolean hasMath = source.contains("$");
      if (hasMath) {
        engine.setOnStatusChanged(event -> adjustHeight(webView, javafxPanel, source));
      }
      else {
        engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
          if (newValue == Worker.State.SUCCEEDED) {
            adjustHeight(webView, javafxPanel, source);
          }
        });
      }
      final BorderPane pane = new BorderPane(webView);
      final String prefix;
      if (hasMath) {
        prefix = String.format(ourMathJaxPrefix, width - 500, EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
      }
      else {
        prefix = String.format(ourPrefix, width - 500);
      }
      final String content = prefix + convertToHtml(source) + ourPostfix;
      engine.loadContent(content);

      final Scene scene = new Scene(pane, 0, 0);

      javafxPanel.setScene(scene);
      updateLaf(LafManager.getInstance().getCurrentLookAndFeel() instanceof DarculaLookAndFeelInfo,
                engine, javafxPanel);
    });

    return javafxPanel;
  }

  private static String convertToHtml(@NotNull String source) {
    //final String result = wrapMath(source);

    //final ExtDecorator decorator = new ExtDecorator();
    //final Configuration.Builder builder = Configuration.builder().forceExtentedProfile()
    //  .registerPlugins(new Plugin[]{new YumlPlugin(), new WebSequencePlugin(), new IncludePlugin()}).setDecorator(decorator)
    //  .setCodeBlockEmitter(new CodeBlockEmitter());
    //String processed = Processor.process(result, builder.build());
    //processed = unwrapMath(processed);

    //return processed;
    return source;
{'fraction_non_alphanumeric': 0.11770388857579905, 'fraction_numerical': 0.0025762821028902667, 'mean_word_length': 1.7069078230551318, 'pattern_counts': {'":': 4, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 102, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136065', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3108, 'n_words': 602}