spm_browser.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function [H, HC] = spm_browser(url,F,pos,format)
  2. % Display an HTML document within a MATLAB figure
  3. % FORMAT H = spm_browser(url,F,pos,[format])
  4. %
  5. % url - string containing URL (e.g. 'http://...' or 'file://...')
  6. % F - figure handle or Tag [Default: Graphics]
  7. % pos - position within figure in pixel units with the format [left,
  8. % bottom, width, height]. [Default: full window with 10px margin]
  9. % format - data format {['html'],'md'}
  10. % 'md' option uses Markdown:
  11. % https://www.wikipedia.org/wiki/Markdown
  12. % http://showdownjs.com/
  13. %
  14. % H - handle to the Java component
  15. % HC - handle to the HG container
  16. %__________________________________________________________________________
  17. % Copyright (C) 2011-2018 Wellcome Trust Centre for Neuroimaging
  18. % Guillaume Flandin
  19. % $Id: spm_browser.m 7478 2018-11-08 14:51:54Z guillaume $
  20. %-Input arguments
  21. %--------------------------------------------------------------------------
  22. if nargin < 1
  23. url = ['file://' fullfile(spm('Dir'),'help','index.html')];
  24. end
  25. if nargin < 2 || isempty(F)
  26. F = spm_figure('GetWin','Graphics');
  27. end
  28. try, isUpdate = ismethod(F,'setCurrentLocation'); catch, isUpdate=false; end
  29. if ~isUpdate, F = spm_figure('FindWin',F); end
  30. if (nargin < 3 || isempty(pos)) && ~isUpdate
  31. u = get(F,'Units');
  32. set(F,'Units','pixels');
  33. pos = get(F,'Position');
  34. set(F,'Units',u);
  35. pos = [10, 10, pos(3)-20, pos(4)-20];
  36. end
  37. if nargin < 4 || isempty(format)
  38. if numel(url) > 1 && strcmp(url(end-2:end),'.md')
  39. format = 'md';
  40. else
  41. format = 'html';
  42. end
  43. end
  44. %-Display
  45. %--------------------------------------------------------------------------
  46. try
  47. % if usejava('awt') && spm_check_version('matlab','7.4') >= 0
  48. %-Create HTML browser panel
  49. %----------------------------------------------------------------------
  50. if ~isUpdate
  51. browser = com.mathworks.mlwidgets.html.HTMLBrowserPanel;
  52. [H, HC] = javacomponent(browser,pos,F);
  53. else
  54. H = F;
  55. HC = [];
  56. end
  57. %-Set content
  58. %----------------------------------------------------------------------
  59. if strcmpi(format,'html') && any(strncmp(url,{'file','http'},4))
  60. H.setCurrentLocation(strrep(url,'\','/'))
  61. elseif strcmpi(format,'md')
  62. H.setHtmlText(md2html(url));
  63. else
  64. H.setHtmlText(url);
  65. end
  66. drawnow;
  67. catch
  68. H = [];
  69. HC = [];
  70. end
  71. %==========================================================================
  72. function html = md2html(md)
  73. % Convert Markdown document into HTML (using Showdown.js)
  74. if exist(md,'file')
  75. md = fileread(md);
  76. elseif any(strncmp(md,{'file','http'},4))
  77. md = urlread(md);
  78. end
  79. md = strrep(md,'\','\\');
  80. md = strrep(md,'"','\"');
  81. md = strrep(md,char(13),'');
  82. md = strrep(md,char(10),'\n');
  83. showdownjs = ['file://' fullfile(spm('Dir'),'help','js','showdown.min.js')];
  84. html = {...
  85. '<!DOCTYPE html>', ....
  86. '<html>', ....
  87. '<head>', ....
  88. '<meta charset="utf-8"/>', ....
  89. '<title>SPM</title>', ....
  90. ['<script src="', showdownjs,'"></script>'], ....
  91. '</head>', ....
  92. '<body>', ....
  93. '<div id="display">Loading...</div>', ....
  94. '<script>', ....
  95. 'var converter = new showdown.Converter();', ....
  96. 'converter.setFlavor("github");', ...
  97. 'converter.setOption("simpleLineBreaks", false);', ...
  98. ['var text = "', md,'";'], ....
  99. 'var html = converter.makeHtml(text);', ....
  100. 'var display = document.getElementById("display");', ....
  101. 'display.innerHTML = html;', ....
  102. '</script>', ....
  103. '</body>', ....
  104. '</html>'};
  105. html = sprintf('%s\n', html{:});