I use notebook "Save as ... (html)" to convert my .nb to web. Formulas and plots get converted to images, which is fine. But code (input cells) get converted too into images. I would like people to be able to copy and paste my code, what can I do?
Not sure how to use (in a generic scenario) what is mentioned in here.
edit Got something very close working:
Export[NotebookDirectory[] <> "/test.html",
EvaluationNotebook[], "HTML",
"ConvertReverseClosed" -> True,
"ConversionRules" -> {"Input" -> {"<code>", DisplayForm[#] &,
"</code>"}}]
if someone can improve on this it will be awesome.
second edit
This is the code I now have in a Palette. The context of the question is here. The code above is now in the proper place to export to HTML, including the CDFs, in (* 1 *).
PaletteNotebook[
Button[Style["Blog it", 12, FontFamily -> "Times"],
Module[{
nb = InputNotebook[],
fpath = NotebookDirectory[InputNotebook[]],
htmlName =
StringDrop[NotebookFileName[InputNotebook[]], -3] <> ".html",
generateCDF,
generateCDFs,
exportHTML
},
generateCDF[nb_, name_] := Module[{sel},
sel = SelectionMove[nb, Next, Cell];
Message[generateCDF::info, name];
Export[
FileNameJoin[{fpath, name}],
Notebook[{NotebookRead[nb]}],
"CDF"];
SelectionMove[nb, "Input", Next, CellStyle]
];
generateCDF::info = "Generating CDF `1`";
generateCDFs[nb_] := Module[{sel, curID},
SelectionMove[nb, Before, Notebook];
sel = NotebookFind[nb, "Input", Next, CellStyle];
While[ MatchQ[sel, _NotebookSelection],
curID = CurrentValue[sel, CellID];
sel = Switch[CurrentValue[sel, CellTags],
_String, generateCDF[nb, CurrentValue[sel, CellTags]],
_, NotebookFind[nb, "Input", Next, CellStyle]
];
];
];
exportHTML[nb_] := Module[{
rules = {
"Input" -> {
"\n<pre class=\"code\">\n",
DisplayForm[#] &,
"\n</pre>\n"
} (* 1 *)
}
},
Message[exportHTML::info, FileNameTake[htmlName]];
Export[
htmlName,
nb,
"HTML",
"CSS" -> None,
"MathOutput" -> "GIF",
"ConvertReverseClosed" -> True,
"ConversionRules" -> rules
];
Message[exportHTML::done];
];
exportHTML::info = "Generating HTML `1`";
exportHTML::done = "Export to HTML done";
generateCDFs[nb];
exportHTML[nb];
],
Appearance -> "Palette"]
]
The problem I have is that it generates funny characters in the HTML. I have tried several "encoding" options without luck. This is an example of what I get:
I am not worried about the tags or formatting as I know how to deal with those in my Rails app or in CSS. But the "wrongly encoded" characters are my issue. I committed the sample.nb and sample palette code to github if any one wants to test it.