├── LICENSE.txt
├── README.md
├── pom.xml
├── screenshot.png
└── src
└── main
└── java
└── com
└── nilhcem
└── md2html
├── App.java
├── console
├── ArgsParser.java
├── ConsoleMode.java
├── DisplayUsageException.java
└── package-info.java
├── gui
├── InputPane.java
├── MainFrame.java
├── MainPanel.java
├── MenuBar.java
├── PreviewPane.java
└── package-info.java
└── package-info.java
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | * Copyright (c) 2011, Gautier
23 | * If the program is launched in GUI mode, sets some properties for a better look and feel integration.
24 | *
23 | *
45 |
46 |
47 | Building it
48 | -----------
49 |
50 | `mvn package`, that's all!
51 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
24 | *
28 | *
53 | * This method is useful to include header and footers. 54 | *
55 | * 56 | * @param filePath the file which should be opened and converted. The file is usually a header or a footer. 57 | * @return the content of the file in parameter, or an empty String if the file is {@code null} or if an error happened. 58 | * @throws FileNotFoundException if the file specified in parameter doesn't exist. 59 | */ 60 | private String getFileContent(String filePath) throws FileNotFoundException { 61 | if (filePath != null) { 62 | File file = new File(filePath); 63 | if (!file.exists()) { 64 | throw new FileNotFoundException(filePath); 65 | } 66 | 67 | try { 68 | return FileUtils.readFileToString(file, "UTF-8"); 69 | } 70 | catch (IOException e) { 71 | System.err.println(String.format("Error parsing file: %s", filePath)); 72 | } 73 | } 74 | return ""; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/console/DisplayUsageException.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.console; 2 | 3 | /** 4 | * Thrown if the program arguments were invalid. 5 | * 6 | * @author Nilhcem 7 | * @since 1.0 8 | */ 9 | public final class DisplayUsageException extends Exception { 10 | private static final long serialVersionUID = -7009963711233684636L; 11 | 12 | /** 13 | * Displays the program usage. 14 | */ 15 | @Override 16 | public String getMessage() { 17 | return "usage: java -jar Markdown2HTML.jar [markdownFile] [- header headerFile.html] [-footer footerFile.html] [-out [file.html]]"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/console/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes used while running the program in command line. 3 | */ 4 | package com.nilhcem.md2html.console; 5 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/InputPane.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.gui; 2 | 3 | import java.awt.event.KeyEvent; 4 | import java.awt.event.KeyListener; 5 | import java.util.Observable; 6 | import javax.swing.JScrollPane; 7 | import javax.swing.JTextArea; 8 | 9 | /** 10 | * Scrolled text area where will be inputed markdown data to be converted. 11 | * 12 | * @author Nilhcem 13 | * @since 1.0 14 | */ 15 | public final class InputPane extends Observable { 16 | private final JScrollPane inputPane = new JScrollPane(); 17 | private final JTextArea inputTextArea = new JTextArea(); 18 | 19 | /** 20 | * Creates the text area and add a key listener to call observer every time a key is released. 21 | */ 22 | public InputPane() { 23 | inputPane.getViewport().add(inputTextArea, null); 24 | inputTextArea.addKeyListener(new KeyListener() { 25 | @Override 26 | public void keyTyped(KeyEvent e) { 27 | } 28 | @Override 29 | public void keyReleased(KeyEvent e) { 30 | setChanged(); 31 | notifyObservers(inputTextArea.getText()); 32 | } 33 | @Override 34 | public void keyPressed(KeyEvent e) { 35 | } 36 | }); 37 | } 38 | 39 | /** 40 | * Returns the JScrollPane object. 41 | * 42 | * @return the JScrollPane object. 43 | */ 44 | public JScrollPane get() { 45 | return inputPane; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/MainFrame.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.gui; 2 | 3 | import java.awt.Dimension; 4 | import javax.swing.JFrame; 5 | 6 | /** 7 | * Provides the main window of the application. 8 | * 9 | * @author Nilhcem 10 | * @since 1.0 11 | */ 12 | public final class MainFrame { 13 | private final JFrame mainFrame = new JFrame("Markdown editor"); 14 | private final MenuBar menu = new MenuBar(); 15 | private final MainPanel panel = new MainPanel(); 16 | 17 | /** 18 | * Creates the main window and makes it visible. 19 | */ 20 | public MainFrame() { 21 | Dimension frameSize = new Dimension(640, 440); 22 | 23 | mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24 | mainFrame.setSize(frameSize); 25 | mainFrame.setMinimumSize(frameSize); 26 | 27 | mainFrame.setJMenuBar(menu.get()); 28 | mainFrame.getContentPane().add(panel.get()); 29 | mainFrame.setLocationRelativeTo(null); // Center main frame 30 | mainFrame.setVisible(true); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/MainPanel.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.gui; 2 | 3 | import javax.swing.JPanel; 4 | import net.miginfocom.swing.MigLayout; 5 | 6 | /** 7 | * Provides the main panel of the application, which will contain all the components. 8 | * 9 | * @author Nilhcem 10 | * @since 1.0 11 | */ 12 | public final class MainPanel { 13 | private final MigLayout layout = new MigLayout( 14 | "", // Layout constraints 15 | "[fill,50%] 10 [fill,50%]", // Column constraints 16 | "[grow,fill]"); // Row constraints 17 | private final JPanel mainPanel = new JPanel(layout); 18 | 19 | private final InputPane input = new InputPane(); 20 | private final PreviewPane preview = new PreviewPane(); 21 | 22 | /** 23 | * Creates the main panel, adding observer to the input and building the GUI. 24 | */ 25 | public MainPanel() { 26 | // Add observer 27 | input.addObserver(preview); 28 | 29 | // Build GUI 30 | mainPanel.add(input.get()); 31 | mainPanel.add(preview.get()); 32 | } 33 | 34 | /** 35 | * Returns the JPanel object. 36 | * 37 | * @return the JPanel object. 38 | */ 39 | public JPanel get() { 40 | return mainPanel; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/MenuBar.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.gui; 2 | 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | import java.util.Observable; 6 | import javax.swing.JMenu; 7 | import javax.swing.JMenuBar; 8 | import javax.swing.JMenuItem; 9 | import javax.swing.JOptionPane; 10 | 11 | /** 12 | * Provides the menu bar of the application. 13 | * 14 | * @author Nilhcem 15 | * @since 1.0 16 | */ 17 | public final class MenuBar extends Observable { 18 | private final JMenuBar menuBar = new JMenuBar(); 19 | 20 | /** 21 | * Creates the menu bar and the different menus (file / edit / help). 22 | */ 23 | public MenuBar() { 24 | menuBar.add(createFileMenu()); 25 | menuBar.add(createHelpMenu()); 26 | } 27 | 28 | /** 29 | * Returns the JMenuBar object. 30 | * 31 | * @return the JMenuBar object. 32 | */ 33 | public JMenuBar get() { 34 | return menuBar; 35 | } 36 | 37 | /** 38 | * Creates the file menu. 39 | *40 | * The file menu contains an "Exit" item, to quit the application. 41 | *
42 | * 43 | * @return the newly created file menu. 44 | */ 45 | private JMenu createFileMenu() { 46 | JMenu fileMenu = new JMenu("File"); 47 | fileMenu.setMnemonic('F'); 48 | 49 | JMenuItem exit = new JMenuItem("Exit"); 50 | exit.setMnemonic('x'); 51 | exit.addActionListener(new ActionListener() { 52 | @Override 53 | public void actionPerformed(ActionEvent e) { 54 | System.exit(0); 55 | } 56 | }); 57 | 58 | fileMenu.add(exit); 59 | return fileMenu; 60 | } 61 | 62 | /** 63 | * Creates the help menu. 64 | *65 | * The help menu contains an "About" item, to display some software information. 66 | *
67 | * 68 | * @return the newly created help menu. 69 | */ 70 | private JMenu createHelpMenu() { 71 | JMenu helpMenu = new JMenu("Help"); 72 | helpMenu.setMnemonic('h'); 73 | 74 | JMenuItem about = new JMenuItem("About"); 75 | about.setMnemonic('a'); 76 | about.addActionListener(new ActionListener() { 77 | @Override 78 | public void actionPerformed(ActionEvent e) { 79 | JOptionPane.showMessageDialog(menuBar.getParent(), 80 | String.format("Extremely simple Markdown to HTML converter%nPowered by MarkdownJ%nhttps://github.com/nilhcem"), 81 | "Markdown2HTML: About", JOptionPane.INFORMATION_MESSAGE); 82 | } 83 | }); 84 | 85 | helpMenu.add(about); 86 | return helpMenu; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/PreviewPane.java: -------------------------------------------------------------------------------- 1 | package com.nilhcem.md2html.gui; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | import javax.swing.JLabel; 6 | import javax.swing.JScrollPane; 7 | import javax.swing.SwingUtilities; 8 | import com.petebevin.markdown.MarkdownProcessor; 9 | 10 | /** 11 | * Scrolled text area where will be displayed the HTML preview. 12 | * 13 | * @author Nilhcem 14 | * @since 1.0 15 | */ 16 | public final class PreviewPane implements Observer { 17 | private final JScrollPane previewPane = new JScrollPane(); 18 | private final JLabel previewLabel = new JLabel(); 19 | 20 | /** 21 | * Creates the HTML JLabel and sets its vertical alignment as in the top. 22 | */ 23 | public PreviewPane() { 24 | previewLabel.setVerticalAlignment(JLabel.TOP); 25 | previewPane.getViewport().add(previewLabel, null); 26 | } 27 | 28 | /** 29 | * Returns the JScrollPane object. 30 | * 31 | * @return the JScrollPane object. 32 | */ 33 | public JScrollPane get() { 34 | return previewPane; 35 | } 36 | 37 | /** 38 | * Updates the content of the label by converting the input data to html and setting them to the label. 39 | *40 | * This method will be called by an {@code InputPane} observable. 41 | *
42 | * 43 | * @param o the observable element which will notify this class. 44 | * @param data a String object containing the input data to be converted into HTML. 45 | */ 46 | @Override 47 | public void update(final Observable o, final Object data) { 48 | if (o instanceof InputPane) { 49 | SwingUtilities.invokeLater(new Runnable() { 50 | @Override 51 | public void run() { 52 | String content = (String)data; 53 | MarkdownProcessor processor = new MarkdownProcessor(); 54 | previewLabel.setText(String.format("%s", processor.markdown(content)).replaceAll("src=\"", "src=\"file:")); // Fix to display images properly. 55 | } 56 | }); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/gui/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes used while running the program in a GUI. 3 | */ 4 | package com.nilhcem.md2html.gui; 5 | -------------------------------------------------------------------------------- /src/main/java/com/nilhcem/md2html/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Entry point of the application. 3 | */ 4 | package com.nilhcem.md2html; 5 | --------------------------------------------------------------------------------