Skip to content

Commit

Permalink
Merge pull request #83 from moacirrf/ft-converter-menu-on-toolbar
Browse files Browse the repository at this point in the history
Include "Export To" to Context Menu
  • Loading branch information
moacirrf authored Dec 22, 2024
2 parents 5621751 + 3c98f2f commit 8760053
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 95 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.moacirrf</groupId>
<artifactId>netbeans-markdown</artifactId>
<version>3.2</version>
<version>4.0</version>
<packaging>nbm</packaging>
<name>Netbeans Markdown</name>
<url>https://github.com/moacirrf/netbeans-markdown</url>
Expand All @@ -22,7 +22,7 @@
<connection>scm:git:https://github.com/moacirrf/netbeans-markdown</connection>
<developerConnection>scm:git:https://github.com/moacirrf/netbeans-markdown</developerConnection>
<url>https://github.com/moacirrf/netbeans-markdown/tree/${project.scm.tag}</url>
<tag>v3.1</tag>
<tag>v4.0</tag>
</scm>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@
@ActionRegistration(
displayName = "#CTL_ExportAction"
)
// path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions",
@ActionReferences(value = {
@ActionReference(path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions", name = "Markdown Exporters", position = 1425), //@ActionReference(path = "Editors/Popup/"+MarkdownDataObject.MIME_TYPE+"/Actions", position = 1425),
// @ActionReference(path = "UI/ToolActions/"+MarkdownDataObject.MIME_TYPE+"/Actions", position = 2950)
@ActionReference(path = "Loaders/" + MarkdownDataObject.MIME_TYPE + "/Actions", name = "Markdown Exporters", position = 1425),
@ActionReference(path = "Editors/" + MarkdownDataObject.MIME_TYPE + "/Popup", position = 9999)
})
@Messages("CTL_ExportAction=Export to...")
public final class ExportAction implements ActionListener {
Expand All @@ -68,8 +67,8 @@ public void actionPerformed(ActionEvent ev) {
JOptionPane.showMessageDialog(null, MESSAGE_SUCCESS);
}
});
var dd = new DialogDescriptor(pane, TITLE_SELECT_DESTINY, true, null);

var dd = new DialogDescriptor(pane, TITLE_SELECT_DESTINY, true, null);
dd.setClosingOptions(new String[0]);
dd.setOptions(new String[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static URL downloadImage(URL url) {

} catch (URISyntaxException | InterruptedException | IOException ex) {
Exceptions.printStackTrace(ex);
if (ex instanceof InterruptedException) {
if (ex instanceof InterruptedException && Thread.currentThread() != null) {
Thread.currentThread().interrupt();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
separatorAfter = 500
),
@ActionReference(
path = "Loaders/text/x-markdown/Actions",
path = "Loaders/"+MarkdownDataObject.MIME_TYPE+"/Actions",
id = @ActionID(category = "Edit", id = "org.openide.actions.DeleteAction"),
position = 600
),
Expand Down Expand Up @@ -102,10 +102,11 @@
public class MarkdownDataObject extends MultiDataObject {

public static final String MIME_TYPE = "text/x-markdown-nb";
public static final String MIME_TYPE_OTHER = "text/x-markdown";

public MarkdownDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
super(pf, loader);
registerEditor("text/x-markdown", true);
registerEditor(MIME_TYPE_OTHER, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class TempDir {

private static Path NOT_LOADED_IMAGE;

public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown";
private static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown";

public static Path getTempDir() {
Path path = Paths.get(TEMP_DIR_PLUGIN);
Expand All @@ -62,7 +62,7 @@ public static Path getCantLoadImage() {
private static void createCantLoadImage() {
try (var inputStream = Icons.class.getResourceAsStream(NB_MARKDOWN_NOT_LOAD_IMAGE)) {
byte[] bytes = inputStream.readAllBytes();
NOT_LOADED_IMAGE = Path.of(TEMP_DIR_PLUGIN, NB_MARKDOWN_NOT_LOAD_IMAGE);
NOT_LOADED_IMAGE = Path.of(getTempDir().toAbsolutePath().toString(), NB_MARKDOWN_NOT_LOAD_IMAGE);
Files.write(NOT_LOADED_IMAGE, bytes, CREATE, TRUNCATE_EXISTING);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.mockStatic;
import org.openide.util.Exceptions;

/**
Expand All @@ -44,6 +48,18 @@ public class ImageHelperTest {
private static final String PNG_HTTP_URL
= "https://raw.githubusercontent.com/moacirrf/netbeans-markdown/main/src/main/resources/io/github/moacirrf/netbeans/markdown/code_template.png";

private MockedStatic<TempDir> mockedStatic;

@Before
public void setup() {
mockedStatic = mockStatic(TempDir.class);
}

@After
public void tearDown() {
mockedStatic.close();
}

@Rule
public TemporaryFolder folder = new TemporaryFolder();

Expand All @@ -60,18 +76,24 @@ public void testIsHttpUrl() throws MalformedURLException {

@Test
public void testDownloadImage() throws MalformedURLException, URISyntaxException {

mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());

URL url = ImageHelper.downloadImage(new URL(SVG_HTTP_URL));
var path = Path.of(url.toURI());

assertNotNull(url);
assertTrue(Files.exists(path));

path.toFile().delete();
TempDirTest.removeTempDir();
}

@Test
public void testConvertSVGToPNG() throws MalformedURLException, URISyntaxException {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());

URL url = ImageHelper.downloadImage(new URL(SVG_HTTP_URL));
URL urlPng = ImageHelper.convertSVGToPNG(url);

Expand All @@ -82,20 +104,23 @@ public void testConvertSVGToPNG() throws MalformedURLException, URISyntaxExcepti
assertTrue(file.exists());

file.delete();
TempDirTest.removeTempDir();
}

@Test
public void testGetImageType(){
public void testGetImageType() {
try {
assertEquals(ImageHelper.getImageType(URI.create(SVG_HTTP_URL).toURL()), "svg"); ;
assertEquals(ImageHelper.getImageType(URI.create(PNG_HTTP_URL).toURL()), "png"); ;
assertEquals(ImageHelper.getImageType(URI.create(SVG_HTTP_URL).toURL()), "svg");;
assertEquals(ImageHelper.getImageType(URI.create(PNG_HTTP_URL).toURL()), "png");;
} catch (MalformedURLException ex) {
Exceptions.printStackTrace(ex);
}
}

@Test
public void testFileExistsByHash() {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());
try {
Path path = folder.newFile("teste.png").toPath();
byte[] bytes = URI.create(PNG_HTTP_URL).toURL().openStream().readAllBytes();
Expand All @@ -105,6 +130,7 @@ public void testFileExistsByHash() {
Files.delete(path);

folder.delete();
TempDirTest.removeTempDir();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2024 Moacir da Roza Flores <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.moacirrf.netbeans.markdown;

import java.io.IOException;
import static java.lang.System.getProperty;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import org.openide.util.Exceptions;

/**
*
* @author Moacir da Roza Flores <[email protected]>
*/
public final class TempDirTest {

private static final String NB_MARKDOWN_NOT_LOAD_IMAGE = "nb_markdown_not_load_image.png";

private static Path NOT_LOADED_IMAGE;

public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown_teste";

public static Path getTempDir() {
Path path = Paths.get(TEMP_DIR_PLUGIN);
if (!Files.exists(path)) {
try {
Files.createDirectory(path);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
createCantLoadImage();
}
return path;
}

public static Path getCantLoadImage() {
if (Files.exists(getTempDir())) {
if (NOT_LOADED_IMAGE == null) {
createCantLoadImage();
}
}
return NOT_LOADED_IMAGE;
}

private static void createCantLoadImage() {
try (var inputStream = Icons.class.getResourceAsStream(NB_MARKDOWN_NOT_LOAD_IMAGE)) {
byte[] bytes = inputStream.readAllBytes();
NOT_LOADED_IMAGE = Path.of(TEMP_DIR_PLUGIN, NB_MARKDOWN_NOT_LOAD_IMAGE);
Files.write(NOT_LOADED_IMAGE, bytes, CREATE, TRUNCATE_EXISTING);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}

public static void removeTempDir() {
clearTempFolder(getTempDir());
}

private static void clearTempFolder(Path path) {
try {
if (Files.isDirectory(path) && Files.list(path).count() > 0) {
Files.list(path).forEach(it -> clearTempFolder(it));
}
path.toFile().setWritable(true);
Files.deleteIfExists(path);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}

private TempDirTest() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
*/
package io.github.moacirrf.netbeans.markdown.export;

import io.github.moacirrf.netbeans.markdown.TempDir;
import io.github.moacirrf.netbeans.markdown.TempDirTest;
import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import static java.util.Arrays.asList;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.mockStatic;
import org.openide.util.Exceptions;

/**
Expand All @@ -36,8 +42,22 @@ public class DocxExporterTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();

private MockedStatic<TempDir> mockedStatic;

@Before
public void setup() {
mockedStatic = mockStatic(TempDir.class);
}

@After
public void tearDown() {
mockedStatic.close();
}

//@Test
public void testExportJoinMds() {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());

var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0));

Expand All @@ -47,12 +67,16 @@ public void testExportJoinMds() {
List<File> files = exporter.export(exporterConfig);
assertFalse("No one docx was generated", files.isEmpty());
files.forEach(f -> assertTrue("File not found", f.exists()));

TempDirTest.removeTempDir();
}

@Test
public void testExportSepratedMds() {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());

var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1) , InputModel.from(getMdfile("test_2.md"), 0));
var mdFiles = asList(InputModel.from(getMdfile("test.md"), 1), InputModel.from(getMdfile("test_2.md"), 0));

var exporterConfig = ExporterConfig.newSeparatedFile(folder.getRoot(), mdFiles);
var exporter = new DocxExporter();
Expand All @@ -61,9 +85,15 @@ public void testExportSepratedMds() {
assertFalse("No one docx was generated", files.isEmpty());
assertTrue("Must have two docx", files.size() == 2);
files.forEach(f -> assertTrue("File not found", f.exists()));

TempDirTest.removeTempDir();

}

public File getMdfile(String name) {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(TempDirTest.getTempDir());
mockedStatic.when(() -> TempDir.getCantLoadImage()).thenReturn(TempDirTest.getCantLoadImage());

try {
var file = Path.of(DocxExporterTest.class.getResource(name).toURI()).toFile();
assertTrue("Md file font exists", file.exists());
Expand Down
Loading

0 comments on commit 8760053

Please sign in to comment.