Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* ::Package:: *)
- (* ::Text:: *)
- (*This file contains the EmbedStylesheet[] and associated functions.*)
- (*Written by Simon Tyler in 2011*)
- (*For contact details see http://stackoverflow.com/users/421225/simon*)
- BeginPackage["EmbedStylesheet`"];
- EmbedStylesheet::usage="EmbedStylesheet[] looks if the current notebook's stylesheet file is in the appropriate subdirectory of $UserBaseDirectory or $BaseDirectory and if it is, then it sets the notebook option StyleDefinitions to the contents of that file.";
- (* ::Subsection:: *)
- (*Code*)
- Begin["`Private`"];
- (* Private function -- could be cleaned up... see notes below *)
- Clear[FindStyle]
- FindStyle::usage="A variant on FindFile[], but made primarily for looking in the locations where style files are normally located.";
- FindStyle[style_String,baseDir_String,includePath:(True|False):False]:=Block[{$Path={baseDir,Select[FileNames["*",baseDir,\[Infinity]],DirectoryQ],If[includePath,$Path,{}]}},(*Print[$Path];*)FindFile[style]]
- FindStyle[style_String,baseDir:($UserBaseDirectory|$BaseDirectory|$InstallationDirectory),includePath:(True|False):False]:=FindStyle[style,FileNameJoin[{baseDir,"SystemFiles","FrontEnd","StyleSheets"}],includePath]
- FindStyle[style_String,baseDir:{__String},includePath:(True|False):False]:=Module[{out=$Failed},Do[out=FindStyle[style,base,includePath];If[out=!=$Failed,Return[out]],{base,baseDir}];out]
- FindStyle[style_String]:=FindStyle[style,{$UserBaseDirectory,$BaseDirectory(*,$InstallationDirectory*)},False]
- FindStyle[style_FrontEnd`FileName,rest___]:=FindStyle[style[[2]],rest]
- Options[EmbedStylesheet]={"AlwaysEmbed"->False, "DefaultLocations"->{$UserBaseDirectory,$BaseDirectory}};
- EmbedStylesheet[opts:OptionsPattern[]]:=Module[{ss,style,file=$Failed,fileIn$Inst=$Failed,
- embed=TrueQ[OptionValue[EmbedStylesheet,opts,"AlwaysEmbed"]],
- locs=OptionValue[EmbedStylesheet,opts,"DefaultLocations"]},
- ss=StyleDefinitions/.Options[EvaluationNotebook[]];
- If[Head[ss]===Notebook,Print["A stylesheet is already embedded."];Return[$Failed]];
- file=FindStyle[ss,locs];
- If[file===$Failed,fileIn$Inst=FindStyle[ss,$InstallationDirectory]];
- Which[
- file=!=$Failed,
- Print["Found file ",file,", setting notebook options..."],
- fileIn$Inst=!=$Failed&&Not[embed],
- Print["Current stylesheet is in the $InstallationDirectory - not embedding."];Return[$Failed],
- fileIn$Inst=!=$Failed&&embed,
- file=fileIn$Inst;Print["Found file ",file,", setting notebook options..."],
- True,
- Print["Can not find the style file in the normal places..."];Return[$Failed]];
- style=Get[file];
- (* Insert check that file is a valid notebook style sheet here! TODO *)
- SetOptions[EvaluationNotebook[], StyleDefinitions->style];
- ]
- End[];
- EndPackage[];
- (* ::Subsection:: *)
- (*Tests and notes*)
- (* ::Subsubsection::Closed:: *)
- (*FindStyle*)
- (* ::Text:: *)
- (*At the moment, I'm searching all subdirectories. *)
- (*This is probably not necessary as the FrontEnd`FileName structure is actually a (relative) directory structure. *)
- (*But ToFileName is being superseded by FileNameJoin which does not work... Neither does FrontEnd`ToFileName*)
- (*It doesn't matter too much, since FindStyle does not get called often and is fast enough*)
- (* ::Input:: *)
- (*Names["FrontEnd`*Name"]*)
- (* ::Input:: *)
- (*FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"]//ToFileName*)
- (* ::Input:: *)
- (*FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"]//FrontEnd`ToFileName*)
- (* ::Input:: *)
- (*FindStyle[FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"],$InstallationDirectory]*)
- (* ::Input:: *)
- (*FindStyle[FrontEnd`FileName[{"Article"},"JournalArticle.nb",CharacterEncoding->"UTF-8"],$InstallationDirectory]*)
- (* ::Subsubsection::Closed:: *)
- (*EmbedStylesheet*)
- (* ::Input:: *)
- (*Exit[]*)
- (* ::Input:: *)
- (*?EmbedStylesheet*)
- (* ::Subsubsubsection::Closed:: *)
- (*"Default.nb"*)
- (* ::Input:: *)
- (*SetOptions[EvaluationNotebook[], StyleDefinitions->"Default.nb"]*)
- (* ::Input:: *)
- (*EmbedStylesheet[]*)
- (* ::Input:: *)
- (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
- (* ::Input:: *)
- (*EmbedStylesheet["AlwaysEmbed"->True]*)
- (* ::Input:: *)
- (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
- (* ::Input:: *)
- (*EmbedStylesheet["AlwaysEmbed"->True]*)
- (* ::Subsubsubsection::Closed:: *)
- (*FrontEnd`FileName[{Article},JournalArticle.nb,CharacterEncoding->UTF-8]*)
- (* ::Input:: *)
- (*SetOptions[EvaluationNotebook[], StyleDefinitions->FrontEnd`FileName[{"Article"},"JournalArticle.nb",CharacterEncoding->"UTF-8"]]*)
- (* ::Input:: *)
- (*EmbedStylesheet[]*)
- (* ::Input:: *)
- (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
- (* ::Subsubsubsection::Closed:: *)
- (*"Math2200Style.nb"*)
- (* ::Input:: *)
- (*SetOptions[EvaluationNotebook[], StyleDefinitions->"Math2200Style.nb"]*)
- (* ::Input:: *)
- (*EmbedStylesheet[]*)
- (* ::Input:: *)
- (*StyleDefinitions/.Options[EvaluationNotebook[]]*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement