#!/usr/bin/perl # knt-html-to-ncd.pl # by Apollia of Astroblahhh.Com. http://astroblahhh.com/ # # Completed April 30, 2011. # # Public domain - except any parts that aren't, such as, quite # possibly, the commify subroutine by Andrew Johnson, which # I found at: # # http://www.perlmonks.org/?node_id=653 # # ######################################################### # # The purpose of this script # # # The purpose of this script is to convert notes from KeyNote NF # 1.7.8.1 - http://code.google.com/p/keynote-nf/ - into the .ncd # format useable by NoteCase 1.9.8 - http://notecase.sourceforge.net/ # - the free note manager software pre-installed in the Lucid Puppy # 5.2 variety of Puppy Linux (and probably other Puppy Linuxes too). # # http://www.murga-linux.com/puppy/viewtopic.php?p=479987 # (Topic: Lucid Puppy 5.2 Official Release JAN 5 2011) # # # I don't know if this script will work properly with other versions # of KeyNote NF and NoteCase besides KeyNote NF 1.7.8.1 and # NoteCase 1.9.8. # # This script can't directly convert .knt format files to .ncd format. # First, you have to use KeyNote NF to export your notes into HTML # format. Then, this script can convert those HTML files into NCD # format. (Actually, NCD format, as you can see if you open an NCD # file in a text editor, is HTML as well - just differently-structured). # # The conversions won't be perfect. For more details, read the # section below titled "Imperfections of Conversions Done By This # Script", after the "Instructions" section. # # To use this script, follow the below instructions. # ################################################## # # Instructions # # # Unfortunately, you probably won't be able to do everything in # Linux. I tried using the Export feature in KeyNote NF in Wine, # but it wouldn't work for me. # # (Wine is "a free software application that aims to allow computer # programs written for Microsoft Windows to run on Unix-like # operating systems" - http://en.wikipedia.org/wiki/Wine_%28software%29 ). # # # Steps to take in Windows # ------------------------ # # * While using Windows, open KeyNote NF. # # * Open the notes file you wish to convert. # # * Go to the File menu and choose Export. # # * Under the "Source and target" tab: # * In the "Source: What to export?" section, click the # radio box next to "All notes". # * Under "Format for exported files:", choose HTML. # * Choose a folder to export files to by clicking the folder icon next # to the text field labeled "Directory for exported files". # # * Under the "Options" tab: # * Make sure there's a check in the box next to # "Include node headings". # * Put this text in the text box below "Include node headings": # --- %L * %D * %N %F %I --- # # * Click the radio box next to "Put all tree nodes in a single # target file" # # * Click the Export button. # # * Open any other note files you wish to export, and export them too. # # * Beware of special characters like bullets (•). Even just one bullet # character can make NoteCase (in both Windows and Linux) refuse to # display any of the text of an entire node, even though the text exists # in the NCD file's source code (though it can easily be overwritten if # you edit that node in NoteCase). # # So, if you have any unusual characters in your KeyNote notes, you should # open your HTML exports in a text editor and use the editor's # Find/Replace capabilities to get rid of them and replace them with # something harmless. For example, you can change bullets to asterisks. # # The few accent characters I've ever had occasion to use, like é (e-acute), # seem to work OK in NoteCase and can be left alone. # # If you forget to remove bullets, etc. from your HTML export files, or # discover after making the conversion that some characters you thought # were safe to use actually weren't, it's also possible to remove bad # characters later on by opening your newly-created NCD files in a text # editor and using the Find/Replace capabilities to replace any # troublesome characters. # # In NoteCase, beware of editing nodes that appear blank but actually # might not be - because if you edit such nodes, you can easily overwrite # the hidden text that may be in the source code of the NCD file. # # * If any of your node titles contain asterisks (*), it might confuse the # script and some of your node titles might get messed up. So, you might # want to change any node titles which contain asterisks. # # * Degree symbols can be displayed by NoteCase in both Windows and Linux. # However, this script might not properly output them, so you'll probably # have to fix them after making the conversion. # # # Steps to take in Linux # ---------------------- # # * Now, you can switch over to Linux if desired, though this script # should run fine in Windows as well. (It worked for me in Windows XP). # # The output differs slightly depending on whether you run this in # Linux or Windows - the paragraph breaks come out differently, and the # output file's size is slightly different, but, overall, the output # looks about the same and seems to work fine. # # * In the source code of this knt-html-to-ncd.pl script, in the # section below titled "Variables you need (or might need) # to change": # # * Set the $folder_of_knt_html_file_to_convert variable # to the path of the folder that contains your KeyNotes in # HTML format. # # * Set $knt_html_file_to_convert to the file name of # whichever HTML file you wish to convert. # # * Optionally: # * You can set $output_file_name and/or $output_folder # to whatever you want. # # * Now, you can run the knt-html-to-ncd.pl script. # # By default, if you don't change $output_folder, your converted # NCD file will be in the same folder as the knt-html-to-ncd.pl # script, in a file titled whatever $output_file_name is set to. # # * In Lucid Puppy 5.2, you can open your shiny new NCD file just by # double-clicking on it. # # (Though, if NoteCase is already open and you have NoteCase configured # to allow only single instances of NoteCase to run, you might have to # close NoteCase first. # # Or, if you'd like to be able to run more than one instance of NoteCase, # then, in NoteCase, go to the Edit menu, then Configure NoteCase, and # in the Startup tab, uncheck "Allow single instance only.") # # In Windows, to open your NoteCase file, you might have to open # NoteCase, go to the File menu, choose Open, then go to your new # NCD file. # # To make it so in Windows, you can open .ncd files in NoteCase just by # double-clicking, right-click your NCD file, choose "Open With", click # Browse, go to the NoteCase program (probably in C:/Program Files/NoteCase), # put a check in the checkbox that says "Always use the selected program # to open this kind of file", and click OK. # # * Your NCD file should be readable on any of the platforms NoteCase # is available on (including Windows, many types of Linux, and MacOS). # # http://notecase.sourceforge.net/ # http://notecase.sourceforge.net/download.html # # ################################################################# # # Imperfections of Conversions Done By This Script # # # The conversions made by this script won't be totally perfect. # # It should properly convert the note tree, and does an OK job with the # formatting of the notes. It should properly handle italics, bold, # and underline (and maybe other styles I don't usually use and haven't # tested, like strikethrough). And, it mostly doesn't mess up the # paragraph breaks too badly. # # # Here are the imperfections I know about. There could easily be # more: # # * There will sometimes be more paragraph breaks than were originally # there. So, a list with adjacent items like this: # # Grape Juice # Potatoes # Oatmeal # # ...will end up looking like this: # # Grape Juice # # Potatoes # # Oatmeal # # * This script doesn't preserve the color of text. (That was # conceivably possible, but it was too hard and I was too impatient). # # * The script removes hyperlinks (not URL text, just the # and HTML codes), since there was some kind of glitch with them # which I was too impatient to try to fix. # # I think that glitch might have to do with the fact that in KeyNote NF # HTML exports, tags appear between the tags instead # of outside them, which I think probably confuses NoteCase. # # * NoteCase permits icons to appear next to node titles, but, # KeyNote NF HTML exports don't contain any information on what icons # you used, so your nodes in NoteCase won't have any icons. # # * Unlike KeyNote NF, NoteCase only lets you use one font and one # font size, and NoteCase doesn't let you center or right-justify # text, so that formatting will be lost. # # * You need to get rid of bullets (•) and probably other unusual # characters in your KeyNotes, because NoteCase (in both Windows and # Linux) refuses to display any of the text in any node that contains # bullets. Some accented characters, like é (e-acute), are fine # (though I haven't tested a wide variety of them). use Cwd 'abs_path'; $this_script_path=abs_path($0); ############################################################################# # # This section of the script, which is only useful in Linux, # makes the script run in a window if double-clicked, if this # script file has Exec permissions, and you have the RXVT terminal # program. (If you're using Lucid Puppy 5.2 or probably various other # forms of Puppy Linux, you'll have RXVT). # # To give this script file Exec permissions in Lucid Puppy 5.2: # using the Rox-Filer file manager, right-click the script file, # and in the File: 'knt-html-to.ncd.pl' menu, select Properties. # Then, put a check in the checkbox across from Owner and under # Exec, and click Close. # # This entire section of code (consisting of the below 3 lines) # can be deleted or commented out if it's not working properly on # your system. if (!$ARGV[0]) # If this script is called with no arguments... { exec ("rxvt -rv +sb -sl 999 -e perl $this_script_path 1"); } # The exec command kills this script and runs the above shell command. # # The above shell command opens an RXVT terminal window, and runs this # Perl script again with one argument (just a meaningless 1 at the end), # which will enable the script to continue past this section. # # The point of this section is to make it so it's possible to read the # script's output without you having had to launch the script manually # from a command line. # # Otherwise, when you double-clicked on this script in your file manager # window, no output would be displayed. # # End of section of the script which makes the script run in a window # if double-clicked. # ########################################################################### ##################################################################### # # Variables you need (or might need) to change # # $folder_of_knt_html_file_to_convert="/mnt/sda3/Puppy/KeyNote HTML Exports/"; # If you're running this in Windows, use forward slashes rather # than backslashes. This format works in Windows: # # D:/Puppy/KeyNote HTML Exports/ # # This format works in Linux: # # /mnt/sda3/Puppy/KeyNote HTML Exports/ $knt_html_file_to_convert="Notes.html"; ################## # # # Variables you can optionally change $output_file_name="knt-html-to-ncd-output-$knt_html_file_to_convert.ncd"; $output_folder=""; # By default, if you leave this blank, the output file will be in # the same folder as the knt-html-to-ncd.pl script. Put in a # folder path if you'd rather have output go to that folder instead. # # # End of variables you need (or might need) to change # ############################################################ #################################################################### # # Variables, etc. you should probably leave alone # # $folder_of_knt_html_file_to_convert=AddSlashIfNeeded($folder_of_knt_html_file_to_convert); # Conveniently adds a slash to the end, if the user forgot. $lastslashpos=rindex($this_script_path, "/"); $script_name=substr($this_script_path, $lastslashpos+1); if ($output_folder eq "") { $output_folder=substr($this_script_path, 0, $lastslashpos); } $output_folder=AddSlashIfNeeded($output_folder); chdir $output_folder; $complete_path_of_knt_html_file_to_convert = $folder_of_knt_html_file_to_convert . $knt_html_file_to_convert; my @file_line; my $numoflines; my $node_depth=-1; my $prev_node_depth=-1; $node_title_line_found="false"; $NODE_TITLE_LINE=111; $OTHER_LINE=222; $LINE_TO_SKIP=333; $NBSP_LINE=444; $BLANK_LINE=555; # # End of variables, etc. you should probably leave alone # #################################################################### main(); sub main { open(INPUTFILE,$complete_path_of_knt_html_file_to_convert); open(RESULTS, ">$output_file_name"); $current_time = time; $modification_timestamp_of_knt_html_file = (stat "$complete_path_of_knt_html_file_to_convert")[9]; print RESULTS " "; while () { if ($just_did_a_node_title_line eq "true") { $just_did_a_node_title_line = "false"; next; } $linestring=$_; $linetype=LineType($linestring); if ($linetype!=$NODE_TITLE_LINE) { if ($node_title_line_found eq "false") { next; } } if ($linetype==$LINE_TO_SKIP) { next; } if ($linetype==$BLANK_LINE) { next; } if ($linetype==$NBSP_LINE) { print RESULTS "\n
\n"; next; } if ($linetype==$OTHER_LINE) { $linestring=~s/

 <\/P>/
/g; $linestring=~s/

//g; $linestring=~s/<\/P>/
\n
/g; $linestring=~s/]*>//g; $linestring=~s/ / /g; $linestring=~s/]*>//g; $linestring=~s/]*>//g; $linestring=~s/<\/A>//g; $linestring=~s/<\/FONT>//g; print RESULTS $linestring; } if ($linetype==$NODE_TITLE_LINE) { if ($node_title_line_found eq "false") { $node_title_line_found="true"; } $just_did_a_node_title_line="true"; @explosion=split("

--- ", $linestring); @explosion2=split("[*]", @explosion[1]); $node_depth=trim(@explosion2[0]); if ($node_depth=="") { $node_depth=0; } #The deeper the node depth, the higher node_depth is. # 0 = top level. # 1 = deeper level # 2 = even deeper level if ($node_depth>$prev_node_depth) # If we're now at a deeper level, make a new

tag { print RESULTS "\n
"; } if ($node_depth<=$prev_node_depth) { # If we're now at a shallower level or the same level... if ($prev_node_depth!=-1) { print RESULTS "\n\n"; } } if ($node_depth<$prev_node_depth) { # If we're now at a shallower level... for ($x=$prev_node_depth;$x>$node_depth;$x--) { if ($prev_node_depth!=-1) { print RESULTS "\n\n"; print RESULTS "\n
"; } } } $node_title=trim(@explosion2[1]); print RESULTS "\n
$node_title
\n"; print RESULTS "\n"; print RESULTS "\n"; print RESULTS "
"; $prev_node_depth=$node_depth; } } } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } sub LineType { $thisline=$_[0]; $thislinelength=length($thisline); if ($thisline eq "\r\n") { return $BLANK_LINE; }; $node_title_line_test=LocateStringIn($thisline,"

--- "); if ($node_title_line_test==0) { return $NODE_TITLE_LINE; }; $skip_line_test=LocateStringIn($thisline,"

 

"); if ($skip_line_test==0) { return $LINE_TO_SKIP; }; if ($thisline eq "

 

\r\n") { return $NBSP_LINE; } return $OTHER_LINE; } sub LocateStringIn { $stringtolookin=$_[0]; $stringtofind=$_[1]; $startofstring=index($stringtolookin,$stringtofind); return $startofstring; } sub AddSlashIfNeeded { $this_string=$_[0]; $lastchar = substr($this_string,length($this_string)-1,1); if ($lastchar ne "/") { $this_string .= "/"; } return $this_string; } ##################################################################### # # # Stuff that gets done at the end of the script: # # # close (INPUTFILE); close (RESULTS); print "\n\n\n"; print "$script_name Perl script finished!\n"; # commify subroutine by Andrew Johnson, found at # http://www.perlmonks.org/?node_id=653 sub commify { my $input = shift; $input = reverse $input; $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; return reverse $input; } $output_file_size = -s $output_file_name; $output_file_size = commify($output_file_size); print "\n\nOutput is in: $output_folder$output_file_name File size: $output_file_size bytes"; print "\n\n\nYou can press Return (or Enter) to close this window.\n"; $wait_for_return = ; # If you've rather have the rxvt window automatically disappear after # several seconds when the script is finished running, comment out # the previous two lines, and uncomment the next lines. # $secs=5; # print "\n\nThis window will automatically close in $secs seconds.\n\n\n"; # sleep $secs; die;