<commit_before>/*
 * Generate HTML from a Mardown source file.
 */
package flow.netbeans.markdown;

import flow.netbeans.markdown.options.MarkdownGlobalOptions;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.pegdown.PegDownProcessor;

@ActionID(category = "File",
        id = "flow.netbeans.markdown.GenerateHtmlAction")
@ActionRegistration(displayName = "#CTL_GenerateHtmlAction", iconBase = "flow/netbeans/markdown/resources/action-export.png")
@ActionReferences({
    @ActionReference(path = "Editors/text/x-markdown/Toolbars/Default", position = 270000),
    @ActionReference(path = "Editors/text/x-markdown/Popup", position = 0),
    @ActionReference(path = "Loaders/text/x-markdown/Actions", position = 250)
})
@Messages("CTL_GenerateHtmlAction=Generate HTML")
public final class MarkdownGenerateHtmlAction implements ActionListener {

    private final MarkdownDataObject context;

    public MarkdownGenerateHtmlAction(MarkdownDataObject context) throws IOException {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        try {
            MarkdownGlobalOptions markdownOptions = MarkdownGlobalOptions.getInstance();
            PegDownProcessor markdownProcessor = new PegDownProcessor(markdownOptions.getExtensionsValue());

            FileObject f = context.getPrimaryFile();
            String fileName = context.getPrimaryFile().getName();
            String saveTo = markdownOptions.isSaveInSourceDir() ? f.getParent().getPath() : "user.home";

            String markdownSource = f.asText();
            String html = markdownProcessor.markdownToHtml(markdownSource);
            String full = markdownOptions.getHtmlTemplate()
                    .replace("{%CONTENT%}", html.toString())
                    .replace("{%TITLE%}", fileName);

            JFileChooser fileChooser = new JFileChooser(saveTo);
            fileChooser.setSelectedFile(new File(fileName + ".html"));
            int option = fileChooser.showSaveDialog(fileChooser);
            int result = 0;<commit_msg>Suggest filename while generating HTML Add option to save in same folder as markdown file by default<commit_after>/*
 * Generate HTML from a Mardown source file.
 */
package flow.netbeans.markdown;

import flow.netbeans.markdown.options.MarkdownGlobalOptions;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.pegdown.PegDownProcessor;

@ActionID(category = "File",
        id = "flow.netbeans.markdown.GenerateHtmlAction")
@ActionRegistration(displayName = "#CTL_GenerateHtmlAction", iconBase = "flow/netbeans/markdown/resources/action-export.png")
@ActionReferences({
    @ActionReference(path = "Editors/text/x-markdown/Toolbars/Default", position = 270000),
    @ActionReference(path = "Editors/text/x-markdown/Popup", position = 0),
    @ActionReference(path = "Loaders/text/x-markdown/Actions", position = 250)
})
@Messages("CTL_GenerateHtmlAction=Generate HTML")
public final class MarkdownGenerateHtmlAction implements ActionListener {

    private final MarkdownDataObject context;

    public MarkdownGenerateHtmlAction(MarkdownDataObject context) throws IOException {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        try {
            MarkdownGlobalOptions markdownOptions = MarkdownGlobalOptions.getInstance();
            PegDownProcessor markdownProcessor = new PegDownProcessor(markdownOptions.getExtensionsValue());

            FileObject f = context.getPrimaryFile();
            String fileName = f.getName();
            String saveTo = markdownOptions.isSaveInSourceDir() ? f.getParent().getPath() : "user.home";

            String markdownSource = f.asText();
            String html = markdownProcessor.markdownToHtml(markdownSource);
            String full = markdownOptions.getHtmlTemplate()
                    .replace("{%CONTENT%}", html.toString())
                    .replace("{%TITLE%}", fileName);

            JFileChooser fileChooser = new JFileChooser(saveTo);
            fileChooser.setSelectedFile(new File(fileName + ".html"));
            int option = fileChooser.showSaveDialog(fileChooser);
            int result = 0;
{'fraction_non_alphanumeric': 0.10364199922510654, 'fraction_numerical': 0.004261913986826811, 'mean_word_length': 4.002906976744186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5829155', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1378, 'n_words': 283}