フォーラム


ゲスト  

ようこそ ゲスト さん。このフォーラムに投稿するには 登録が必要です。

ページ: [1]
トピック: Delphi のソースコードをシンタックスハイライトで資料に貼り付けたい
DEKO
管理者
投稿数: 2690
Delphi のソースコードをシンタックスハイライトで資料に貼り付けたい
on: 2017/01/31 17:31 Tue

[パワーポイントとかの資料にはシンタックスハイライト付きでソースを例示したいですよね。(Qiita)]
http://qiita.com/kazinoue/items/3800f1d708f8969fdb15

比較的簡単に、しかもカスタマイズ可能なのを作ってみました。
使い方は readme.txt をご覧ください。

Download: http://ht-deko.com/software/LocalHilighter_100.zip

DEKO
管理者
投稿数: 2690
Re: Delphi のソースコードをシンタックスハイライトで資料に貼り付けたい
on: 2017/02/01 11:46 Wed

ガワを Delphi で作ってみました。
上にソースコード貼り付けて [Convert] ボタン押すとクリップボードに構文強調表示されたソースコードが入ります。

ソースコードはこんな感じです。アーカイブにプロジェクト一式が含まれていますので、気に入らないトコロがあったらご自由に改変してお使いください。

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.OleCtrls, SHDocVw, System.NetEncoding;

type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
btnConvert: TButton;
Memo1: TMemo;
cbLanguage: TComboBox;
procedure btnConvertClick(Sender: TObject);
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConvertClick(Sender: TObject);
// ドキュメントの生成&TWebBrowser へ読み込み
var
Document: TStringList;
htmlFile: string;
begin
// Temporary html
htmlFile := ChangeFileExt(Application.ExeName, '.html');

// Generate HTML
Document := TStringList.Create;
try
Document.Add('<!DOCTYPE html>' );
Document.Add('<html lang="ja">' );
Document.Add(' <head>' );
Document.Add(' <meta http-equiv="content-type" content="text/css; charset=UTF-8">' );
Document.Add(' <link rel="stylesheet" href="./styles/radstudio.css">' );
Document.Add(' <script src="./highlight.pack.js"></script>' );
Document.Add(' </head>' );
Document.Add('<body>' );
Document.Add('<pre style="font-family: ''MS ゴシック''; font-size: 10pt; letter-spacing: 1px;">');

// Language
Document.Add('<code class="' + LowerCase(cbLanguage.Text) + '">');
// Source Code
Document.Add(TNetEncoding.HTML.Encode(Memo1.Lines.Text));

Document.Add('</code></pre>' );
Document.Add('<script type="text/javascript">hljs.initHighlightingOnLoad();</script>' );
Document.Add('</body>' );
Document.Add('</html>' );

// Save
Document.SaveToFile(htmlFile, TEncoding.UTF8);
finally
Document.Free;
end;

// TWebBrowser
WebBrowser1.Navigate2(htmlFile);
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
// ドキュメントが読み込まれたらクリップボードへコピー
var
pvaIn, pvaOut: OleVariant;
begin
pvaIn := EmptyParam;
pvaOut := EmptyParam;
WebBrowser1.ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_PROMPTUSER, pvaIn, pvaOut);
WebBrowser1.ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DONTPROMPTUSER, pvaIn);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
// Ctrl+A で全選択
begin
if Key = ^A then
begin
Key := #$00;
Memo1.SelectAll;
end;
end;

end.

 
※ 詳しくは readme.txt をお読みください。

DEKO
管理者
投稿数: 2690
Src2Doc ver 1.10
on: 2017/02/01 12:13 Wed

ちょっと修正。

DEKO
管理者
投稿数: 2690
Re: Delphi のソースコードをシンタックスハイライトで資料に貼り付けたい
on: 2017/02/01 12:16 Wed

レジストリファイルは

[TWebBrowser (IE コンポーネント) と互換モード]
http://ht-deko.com/delphiforum/?vasthtmlaction=viewtopic&t=1109.0

この件を回避するのに使われています。これを行わないと JavaScript の実行時にエラーが出ます。

DEKO
管理者
投稿数: 2690
Src2Doc ver 1.20
on: 2017/02/01 13:14 Wed
  • 言語選択コンボボックスを追加

Download: http://ht-deko.com/software/src2doc_120.zip

ページ: [1]
WP Forum Server by ForumPress | LucidCrew
バージョン: 1.7.5 ; ページロード: 0.029 sec.