Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

EmbedStylesheet.m

By: simonjtyler on Sep 19th, 2011  |  syntax: None  |  size: 5.05 KB  |  views: 140  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (* ::Package:: *)
  2.  
  3. (* ::Text:: *)
  4. (*This file contains the EmbedStylesheet[] and associated functions.*)
  5. (*Written by Simon Tyler in 2011*)
  6. (*For contact details see http://stackoverflow.com/users/421225/simon*)
  7.  
  8.  
  9. BeginPackage["EmbedStylesheet`"];
  10.  
  11.  
  12. 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.";
  13.  
  14.  
  15. (* ::Subsection:: *)
  16. (*Code*)
  17.  
  18.  
  19. Begin["`Private`"];
  20.  
  21.  
  22. (* Private function -- could be cleaned up... see notes below *)
  23. Clear[FindStyle]
  24. FindStyle::usage="A variant on FindFile[], but made primarily for looking in the locations where style files are normally located.";
  25. FindStyle[style_String,baseDir_String,includePath:(True|False):False]:=Block[{$Path={baseDir,Select[FileNames["*",baseDir,\[Infinity]],DirectoryQ],If[includePath,$Path,{}]}},(*Print[$Path];*)FindFile[style]]
  26. FindStyle[style_String,baseDir:($UserBaseDirectory|$BaseDirectory|$InstallationDirectory),includePath:(True|False):False]:=FindStyle[style,FileNameJoin[{baseDir,"SystemFiles","FrontEnd","StyleSheets"}],includePath]
  27. 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]
  28. FindStyle[style_String]:=FindStyle[style,{$UserBaseDirectory,$BaseDirectory(*,$InstallationDirectory*)},False]
  29. FindStyle[style_FrontEnd`FileName,rest___]:=FindStyle[style[[2]],rest]
  30.  
  31.  
  32. Options[EmbedStylesheet]={"AlwaysEmbed"->False, "DefaultLocations"->{$UserBaseDirectory,$BaseDirectory}};
  33.  
  34.  
  35. EmbedStylesheet[opts:OptionsPattern[]]:=Module[{ss,style,file=$Failed,fileIn$Inst=$Failed,
  36.                 embed=TrueQ[OptionValue[EmbedStylesheet,opts,"AlwaysEmbed"]],
  37.                 locs=OptionValue[EmbedStylesheet,opts,"DefaultLocations"]},
  38.         ss=StyleDefinitions/.Options[EvaluationNotebook[]];
  39.         If[Head[ss]===Notebook,Print["A stylesheet is already embedded."];Return[$Failed]];
  40.         file=FindStyle[ss,locs];
  41.         If[file===$Failed,fileIn$Inst=FindStyle[ss,$InstallationDirectory]];
  42.         Which[
  43.                 file=!=$Failed,
  44.                 Print["Found file ",file,", setting notebook options..."],
  45.                 fileIn$Inst=!=$Failed&&Not[embed],
  46.                 Print["Current stylesheet is in the $InstallationDirectory - not embedding."];Return[$Failed],
  47.                 fileIn$Inst=!=$Failed&&embed,
  48.                 file=fileIn$Inst;Print["Found file ",file,", setting notebook options..."],
  49.                 True,
  50.                 Print["Can not find the style file in the normal places..."];Return[$Failed]];
  51.         style=Get[file];
  52.         (* Insert check that file is a valid notebook style sheet here! TODO *)
  53.         SetOptions[EvaluationNotebook[], StyleDefinitions->style];
  54.         ]
  55.  
  56.  
  57. End[];
  58.  
  59.  
  60. EndPackage[];
  61.  
  62.  
  63. (* ::Subsection:: *)
  64. (*Tests and notes*)
  65.  
  66.  
  67. (* ::Subsubsection::Closed:: *)
  68. (*FindStyle*)
  69.  
  70.  
  71. (* ::Text:: *)
  72. (*At the moment, I'm searching all subdirectories. *)
  73. (*This is probably not necessary as the FrontEnd`FileName structure is actually a (relative) directory structure. *)
  74. (*But ToFileName is being superseded by FileNameJoin which does not work... Neither does FrontEnd`ToFileName*)
  75. (*It doesn't matter too much, since FindStyle does not get called often and is fast enough*)
  76.  
  77.  
  78. (* ::Input:: *)
  79. (*Names["FrontEnd`*Name"]*)
  80.  
  81.  
  82. (* ::Input:: *)
  83. (*FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"]//ToFileName*)
  84.  
  85.  
  86. (* ::Input:: *)
  87. (*FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"]//FrontEnd`ToFileName*)
  88.  
  89.  
  90. (* ::Input:: *)
  91. (*FindStyle[FrontEnd`FileName[{"Utility"},"Correspondence.nb",CharacterEncoding->"UTF-8"],$InstallationDirectory]*)
  92.  
  93.  
  94. (* ::Input:: *)
  95. (*FindStyle[FrontEnd`FileName[{"Article"},"JournalArticle.nb",CharacterEncoding->"UTF-8"],$InstallationDirectory]*)
  96.  
  97.  
  98. (* ::Subsubsection::Closed:: *)
  99. (*EmbedStylesheet*)
  100.  
  101.  
  102. (* ::Input:: *)
  103. (*Exit[]*)
  104.  
  105.  
  106. (* ::Input:: *)
  107. (*?EmbedStylesheet*)
  108.  
  109.  
  110. (* ::Subsubsubsection::Closed:: *)
  111. (*"Default.nb"*)
  112.  
  113.  
  114. (* ::Input:: *)
  115. (*SetOptions[EvaluationNotebook[], StyleDefinitions->"Default.nb"]*)
  116.  
  117.  
  118. (* ::Input:: *)
  119. (*EmbedStylesheet[]*)
  120.  
  121.  
  122. (* ::Input:: *)
  123. (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
  124.  
  125.  
  126. (* ::Input:: *)
  127. (*EmbedStylesheet["AlwaysEmbed"->True]*)
  128.  
  129.  
  130. (* ::Input:: *)
  131. (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
  132.  
  133.  
  134. (* ::Input:: *)
  135. (*EmbedStylesheet["AlwaysEmbed"->True]*)
  136.  
  137.  
  138. (* ::Subsubsubsection::Closed:: *)
  139. (*FrontEnd`FileName[{Article},JournalArticle.nb,CharacterEncoding->UTF-8]*)
  140.  
  141.  
  142. (* ::Input:: *)
  143. (*SetOptions[EvaluationNotebook[], StyleDefinitions->FrontEnd`FileName[{"Article"},"JournalArticle.nb",CharacterEncoding->"UTF-8"]]*)
  144.  
  145.  
  146. (* ::Input:: *)
  147. (*EmbedStylesheet[]*)
  148.  
  149.  
  150. (* ::Input:: *)
  151. (*StyleDefinitions/.Options[EvaluationNotebook[]]//Short[#,5]&*)
  152.  
  153.  
  154. (* ::Subsubsubsection::Closed:: *)
  155. (*"Math2200Style.nb"*)
  156.  
  157.  
  158. (* ::Input:: *)
  159. (*SetOptions[EvaluationNotebook[], StyleDefinitions->"Math2200Style.nb"]*)
  160.  
  161.  
  162. (* ::Input:: *)
  163. (*EmbedStylesheet[]*)
  164.  
  165.  
  166. (* ::Input:: *)
  167. (*StyleDefinitions/.Options[EvaluationNotebook[]]*)