Search in sources :

Example 6 with Parser

use of org.commonmark.parser.Parser in project Slide by ccrama.

the class DoEditorActions method doActions.

public static void doActions(final EditText editText, final View baseView, final FragmentManager fm, final Activity a, final String oldComment, @Nullable final String[] authors) {
    baseView.findViewById(R.id.bold).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("**", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "**");
                editText.getText().insert(pos + 1, "**");
                // put the cursor between the symbols
                editText.setSelection(pos + 2);
            }
        }
    });
    if (baseView.findViewById(R.id.author) != null) {
        if (authors != null && authors.length > 0) {
            baseView.findViewById(R.id.author).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (authors.length == 1) {
                        int pos = editText.getSelectionStart();
                        String author = "/u/" + authors[0];
                        editText.setText(editText.getText().toString() + author);
                        // put the cursor between the symbols
                        editText.setSelection(pos + author.length());
                    } else {
                        new AlertDialogWrapper.Builder(a).setTitle(R.string.authors_above).setItems(authors, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int pos = editText.getSelectionStart();
                                String author = "/u/" + authors[which];
                                editText.setText(editText.getText().toString() + author);
                                // put the cursor between the symbols
                                editText.setSelection(pos + author.length());
                            }
                        }).setNeutralButton(R.string.btn_cancel, null).show();
                    }
                }
            });
        } else {
            baseView.findViewById(R.id.author).setVisibility(View.GONE);
        }
    }
    baseView.findViewById(R.id.italics).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("*", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "*");
                editText.getText().insert(pos + 1, "*");
                // put the cursor between the symbols
                editText.setSelection(pos + 1);
            }
        }
    });
    baseView.findViewById(R.id.strike).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("~~", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "~~");
                editText.getText().insert(pos + 1, "~~");
                // put the cursor between the symbols
                editText.setSelection(pos + 2);
            }
        }
    });
    baseView.findViewById(R.id.savedraft).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Drafts.addDraft(editText.getText().toString());
            Snackbar s = Snackbar.make(baseView.findViewById(R.id.savedraft), "Draft saved", Snackbar.LENGTH_SHORT);
            View view = s.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(Color.WHITE);
            s.setAction(R.string.btn_discard, new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Drafts.deleteDraft(Drafts.getDrafts().size() - 1);
                }
            });
            s.show();
        }
    });
    baseView.findViewById(R.id.draft).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final ArrayList<String> drafts = Drafts.getDrafts();
            Collections.reverse(drafts);
            final String[] draftText = new String[drafts.size()];
            for (int i = 0; i < drafts.size(); i++) {
                draftText[i] = drafts.get(i);
            }
            if (drafts.isEmpty()) {
                new AlertDialogWrapper.Builder(a).setTitle(R.string.dialog_no_drafts).setMessage(R.string.dialog_no_drafts_msg).setPositiveButton(R.string.btn_ok, null).show();
            } else {
                new AlertDialogWrapper.Builder(a).setTitle(R.string.choose_draft).setItems(draftText, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        editText.setText(editText.getText().toString() + draftText[which]);
                    }
                }).setNeutralButton(R.string.btn_cancel, null).setPositiveButton(R.string.manage_drafts, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final boolean[] selected = new boolean[drafts.size()];
                        new AlertDialogWrapper.Builder(a).setTitle(R.string.choose_draft).setNeutralButton(R.string.btn_cancel, null).alwaysCallMultiChoiceCallback().setNegativeButton(R.string.btn_delete, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new AlertDialogWrapper.Builder(a).setTitle(R.string.really_delete_drafts).setCancelable(false).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        ArrayList<String> draf = new ArrayList<>();
                                        for (int i = 0; i < draftText.length; i++) {
                                            if (!selected[i]) {
                                                draf.add(draftText[i]);
                                            }
                                        }
                                        Drafts.save(draf);
                                    }
                                }).setNegativeButton(R.string.btn_no, null).show();
                            }
                        }).setMultiChoiceItems(draftText, selected, new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                selected[which] = isChecked;
                            }
                        }).show();
                    }
                }).show();
            }
        }
    });
    /*todo baseView.findViewById(R.id.strikethrough).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wrapString("~~", editText);
            }
        });*/
    baseView.findViewById(R.id.imagerep).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            e = editText.getText();
            sStart = editText.getSelectionStart();
            sEnd = editText.getSelectionEnd();
            TedBottomPicker tedBottomPicker = new TedBottomPicker.Builder(editText.getContext()).setOnImageSelectedListener(new TedBottomPicker.OnImageSelectedListener() {

                @Override
                public void onImageSelected(List<Uri> uri) {
                    handleImageIntent(uri, editText, a);
                }
            }).setLayoutResource(R.layout.image_sheet_dialog).setTitle("Choose a photo").create();
            tedBottomPicker.show(fm);
            InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    });
    baseView.findViewById(R.id.draw).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (SettingValues.tabletUI) {
                doDraw(a, editText, fm);
            } else {
                AlertDialogWrapper.Builder b = new AlertDialogWrapper.Builder(a).setTitle(R.string.general_cropdraw_ispro).setMessage(R.string.pro_upgrade_msg).setPositiveButton(R.string.btn_yes_exclaim, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        try {
                            a.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=me.ccrama.slideforreddittabletuiunlock")));
                        } catch (ActivityNotFoundException e) {
                            a.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=me.ccrama.slideforreddittabletuiunlock")));
                        }
                    }
                }).setNegativeButton(R.string.btn_no_danks, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });
                if (SettingValues.previews > 0) {
                    b.setNeutralButton(a.getString(R.string.pro_previews, SettingValues.previews), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            SettingValues.prefs.edit().putInt(SettingValues.PREVIEWS_LEFT, SettingValues.previews - 1).apply();
                            SettingValues.previews = SettingValues.prefs.getInt(SettingValues.PREVIEWS_LEFT, 10);
                            doDraw(a, editText, fm);
                        }
                    });
                }
                b.show();
            }
        }
    });
    /*todo baseView.findViewById(R.id.superscript).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertBefore("^", editText);
            }
        });*/
    baseView.findViewById(R.id.size).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("#", editText);
        }
    });
    baseView.findViewById(R.id.quote).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (oldComment != null) {
                final TextView showText = new TextView(a);
                showText.setText(oldComment);
                showText.setTextIsSelectable(true);
                int sixteen = Reddit.dpToPxVertical(24);
                showText.setPadding(sixteen, 0, sixteen, 0);
                AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(a);
                builder.setView(showText).setTitle(R.string.editor_actions_quote_comment).setCancelable(true).setPositiveButton(a.getString(R.string.btn_select), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String selected = showText.getText().toString().substring(showText.getSelectionStart(), showText.getSelectionEnd());
                        if (selected.equals("")) {
                            insertBefore("> " + oldComment, editText);
                        } else {
                            insertBefore("> " + selected + "\n\n", editText);
                        }
                    }
                }).setNegativeButton(a.getString(R.string.btn_cancel), null).show();
            } else {
                insertBefore("> ", editText);
            }
        }
    });
    baseView.findViewById(R.id.bulletlist).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("* ", editText);
        }
    });
    baseView.findViewById(R.id.numlist).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("1. ", editText);
        }
    });
    baseView.findViewById(R.id.preview).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<Extension> extensions = Arrays.asList(TablesExtension.create(), StrikethroughExtension.create());
            Parser parser = Parser.builder().extensions(extensions).build();
            HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
            Node document = parser.parse(editText.getText().toString());
            String html = renderer.render(document);
            LayoutInflater inflater = a.getLayoutInflater();
            final View dialoglayout = inflater.inflate(R.layout.parent_comment_dialog, null);
            final AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(a);
            setViews(html, "NO sub", (SpoilerRobotoTextView) dialoglayout.findViewById(R.id.firstTextView), (CommentOverflow) dialoglayout.findViewById(R.id.commentOverflow));
            builder.setView(dialoglayout);
            builder.show();
        }
    });
    baseView.findViewById(R.id.link).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final LayoutInflater inflater = LayoutInflater.from(a);
            final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.insert_link, null);
            int[] attrs = { R.attr.fontColor };
            TypedArray ta = baseView.getContext().obtainStyledAttributes(new ColorPreferences(baseView.getContext()).getFontStyle().getBaseId(), attrs);
            ta.recycle();
            String selectedText = "";
            // if the user highlighted text before inputting a URL, use that text for the descriptionBox
            if (editText.hasSelection()) {
                final int startSelection = editText.getSelectionStart();
                final int endSelection = editText.getSelectionEnd();
                selectedText = editText.getText().toString().substring(startSelection, endSelection);
            }
            final boolean selectedTextNotEmpty = !selectedText.isEmpty();
            final MaterialDialog dialog = new MaterialDialog.Builder(editText.getContext()).title(R.string.editor_title_link).customView(layout, false).positiveColorAttr(R.attr.tintColor).positiveText(R.string.editor_action_link).onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    final EditText urlBox = (EditText) dialog.findViewById(R.id.url_box);
                    final EditText textBox = (EditText) dialog.findViewById(R.id.text_box);
                    dialog.dismiss();
                    final String s = "[".concat(textBox.getText().toString()).concat("](").concat(urlBox.getText().toString()).concat(")");
                    int start = Math.max(editText.getSelectionStart(), 0);
                    int end = Math.max(editText.getSelectionEnd(), 0);
                    editText.getText().insert(Math.max(start, end), s);
                    // delete the selected text to avoid duplication
                    if (selectedTextNotEmpty) {
                        editText.getText().delete(start, end);
                    }
                }
            }).build();
            // Tint the hint text if the base theme is Sepia
            if (SettingValues.currentTheme == 5) {
                ((EditText) dialog.findViewById(R.id.url_box)).setHintTextColor(ContextCompat.getColor(dialog.getContext(), R.color.md_grey_600));
                ((EditText) dialog.findViewById(R.id.text_box)).setHintTextColor(ContextCompat.getColor(dialog.getContext(), R.color.md_grey_600));
            }
            // use the selected text as the text for the link
            if (!selectedText.isEmpty()) {
                ((EditText) dialog.findViewById(R.id.text_box)).setText(selectedText);
            }
            dialog.show();
        }
    });
    try {
        ((ImageInsertEditText) editText).setImageSelectedCallback(new ImageInsertEditText.ImageSelectedCallback() {

            @Override
            public void onImageSelected(final Uri content, String mimeType) {
                e = editText.getText();
                sStart = editText.getSelectionStart();
                sEnd = editText.getSelectionEnd();
                handleImageIntent(new ArrayList<Uri>() {

                    {
                        add(content);
                    }
                }, editText, a);
            }
        });
    } catch (Exception e) {
    // if thrown, there is likely an issue implementing this on the user's version of Android. There shouldn't be an issue, but just in case
    }
}
Also used : DialogInterface(android.content.DialogInterface) Node(org.commonmark.node.Node) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) Uri(android.net.Uri) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TypedArray(android.content.res.TypedArray) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) List(java.util.List) ArrayList(java.util.ArrayList) EditText(android.widget.EditText) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ColorPreferences(me.ccrama.redditslide.ColorPreferences) Intent(android.content.Intent) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) View(android.view.View) TextView(android.widget.TextView) FileNotFoundException(java.io.FileNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) IOException(java.io.IOException) Parser(org.commonmark.parser.Parser) SubmissionParser(me.ccrama.redditslide.util.SubmissionParser) ActivityNotFoundException(android.content.ActivityNotFoundException) DialogAction(com.afollestad.materialdialogs.DialogAction) LayoutInflater(android.view.LayoutInflater) HtmlRenderer(org.commonmark.html.HtmlRenderer) TedBottomPicker(gun0912.tedbottompicker.TedBottomPicker) LinearLayout(android.widget.LinearLayout) Snackbar(android.support.design.widget.Snackbar)

Example 7 with Parser

use of org.commonmark.parser.Parser in project codeu-2018-team12 by codeu-2018-team12.

the class TextFormatter method formatForDisplay.

public static String formatForDisplay(String input) {
    // Parses input and replaces markdown characters with the appropriate HTML
    Parser parser = Parser.builder().build();
    Node document = parser.parse(input);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    String parsedInput = renderer.render(document);
    // Removes unneeded <p> tags
    int parsedInputLength = parsedInput.length();
    if (parsedInput.startsWith("<p>")) {
        parsedInput = parsedInput.substring(3, parsedInputLength - 5);
    }
    // Ensures that new line characters are displayed correctly
    parsedInput = parsedInput.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
    // Replaces emoji aliases with Hexadecimal values ready for display in HTML
    String finalOutput = EmojiParser.parseToUnicode(parsedInput);
    finalOutput = EmojiParser.parseToHtmlHexadecimal(finalOutput);
    return finalOutput;
}
Also used : HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser) EmojiParser(com.vdurmont.emoji.EmojiParser)

Example 8 with Parser

use of org.commonmark.parser.Parser in project xian by happyyangyuan.

the class MdToHtml method mdToHtml.

/**
 * @param md the markdown string.
 * @return converted html
 */
public static String mdToHtml(String md) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(md);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    return renderer.render(document);
}
Also used : Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Example 9 with Parser

use of org.commonmark.parser.Parser in project lavagna by digitalfondue.

the class MailTicketService method sendEmail.

private void sendEmail(String to, String name, Card createdCard, Board board, ProjectMailTicketConfig config, ProjectMailTicket ticketConfig) {
    String cardId = board.getShortName() + "-" + createdCard.getSequence();
    String subjectTemplate = "" + (ticketConfig.getNotificationOverride() ? ticketConfig.getSubject() : config.getSubject());
    String bodyTemplate = "" + (ticketConfig.getNotificationOverride() ? ticketConfig.getBody() : config.getBody());
    String subject = subjectTemplate.replaceAll("\\{\\{card}}", cardId);
    String body = bodyTemplate.replaceAll("\\{\\{card}}", cardId).replaceAll("\\{\\{name}}", name != null ? name : to);
    Parser parser = Parser.builder().build();
    Node document = parser.parse(body);
    HtmlRenderer htmlRenderer = HtmlRenderer.builder().build();
    TextContentRenderer textRendered = TextContentRenderer.builder().build();
    String htmlText = htmlRenderer.render(document);
    String plainText = textRendered.render(document);
    ProjectMailTicketConfigData configData = config.getConfig();
    MailConfig mailConfig = new MailConfig(configData.getOutboundServer(), configData.getOutboundPort(), configData.getOutboundProtocol(), configData.getOutboundUser(), configData.getOutboundPassword(), ticketConfig.getSendByAlias() ? ticketConfig.getAlias() : configData.getOutboundAddress(), configData.getOutboundProperties());
    mailConfig.send(to, subject, plainText, htmlText);
}
Also used : Node(org.commonmark.node.Node) TextContentRenderer(org.commonmark.renderer.text.TextContentRenderer) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Example 10 with Parser

use of org.commonmark.parser.Parser in project mdw-designer by CenturyLinkCloud.

the class MarkdownRenderer method renderHtml.

public String renderHtml() {
    if (markdown == null || markdown.trim().isEmpty())
        return "";
    else {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return "<div style=\"height:100%;overflow:auto;font-family:'Helvetica Neue', Helvetica, Arial, sans-serif\">" + renderer.render(document) + "</div>";
    }
}
Also used : Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Aggregations

Parser (org.commonmark.parser.Parser)12 Node (org.commonmark.node.Node)11 HtmlRenderer (org.commonmark.renderer.html.HtmlRenderer)7 IOException (java.io.IOException)2 Extension (org.commonmark.Extension)2 TablesExtension (org.commonmark.ext.gfm.tables.TablesExtension)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 Snackbar (android.support.design.widget.Snackbar)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 EditText (android.widget.EditText)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)1