Netbeans RTF Copy plugin

I made a little plugin for Netbeans to allow copying text from the editor as RTF; mainly, preserving the foreground colors from its syntax highlighting. I wanted something akin to what’s possible with Visual Studio.

Download plugin

Source at bitbucket

netbeans copy as rtf

Taking the copied RTF text and running it thru Rtf2Html, I can then post nicely colored code:

header('Content-type: application/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";

The NBSpecialCopyPaste by Casper Bang was incredibly helpful as a starting point for this plugin.

There’s an issue I wasn’t able to resolve before releasing this; fields (by default, highlighted green by the editor) aren’t copied properly in some cases. The API seems to give no indication that these tokens are colored differently. I know this is true for Java and PHP code, but not true for XML. The code to get the necessary AttributeSet is exactly what’s in NBSpecialCopyPaste:

private static AttributeSet findFontAndColors(Token tkn, FontColorSettings fcs)
{
AttributeSet as = fcs.getTokenFontColors( tkn.id().name() );
if (as == null)
{
// ...try to get from its category
as = fcs.getTokenFontColors(tkn.id().primaryCategory());
}

return as;
}

I’ll dig deeper and try to resolve this in a future version.

10 Comments

  1. What is the difference between using NbRtfCopy/Rtf2Html and using print to HTML file (on a Mac)?

  2. Avishkar Autar

    Well, first, Rtf2Html can’t be used on a Mac, I wrote it in .NET way back when… never really thought of porting it, but I guess it could be done easily enough.

    Anyways, Netbeans’ Print to HTML works great if I need to generate a full HTML page, but I almost never do that. I usually post snippets that need to be embedded within an existing HTML source (e.g. this blog post); in such a case, there are 2 clear benefits:

    1. It saves me from having to strip out the unnecessary doctype, html, head, body, etc. tags

    2. I don’t have to refactor or inline the CSS produced by Netbeans; Rtf2Html does all styling inline. The CSS that Netbeans produces is a potential problem because it outputs CSS classes with fairly generic names (e.g. comment, line-number) which could easily conflict with other elements on the page.

  3. Rob

    Thanks for this, it should be a real help when I’m writing docs about my code!

  4. […] around the goal of being able to quickly copy and paste snippets of code from Visual Studio (or Netbeans) and turning it into HTML that I could embed in these blog posts; this update stays true to that, […]

  5. Hi,

    Does this plugin work with NetBeans 7?

    Regards,

    Rob,,,

  6. To answer my own question: yes it does!

    Thank you!

  7. Avishkar Autar

    Awesome. I haven’t upgraded as yet.
    Good to know.

  8. apokanou

    Thanks for this, it should be a real help when I’m writing docs about my code!

  9. Cary

    Update as of August 2015, this plugin doesn’t work on windows 8.1.

  10. Avishkar Autar

    Working for me on Windows 10 with Netbeans 8.0.2

Leave a Reply