⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synhighlighteruri.pas

📁 用delphi写的delphi源代码 用delphi写的delphi源代码 用delphi写的delphi源代码 用delphi写的delphi源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: SynHighlighterURI.pas, released 2003-04-10.
The initial author of this file is Ma雔 H鰎z.
All Rights Reserved.

Contributors to the SynEdit project are listed in the Contributors.txt file.

Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.

$Id: SynHighlighterURI.pas,v 1.17 2004/09/03 10:52:41 maelh Exp $

You may retrieve the latest version of SynEdit from the SynEdit home page,
located at http://SynEdit.SourceForge.net

-------------------------------------------------------------------------------}
{
@abstract(Provides an URI syntax highlighter for SynEdit)
@author(Ma雔 H鰎z)
@created(2003)
@lastmod(2003-10-21)
http://www.mh-nexus.de

The SynHighlighterURI unit implements an URI syntax highlighter for SynEdit.

Recognition of URIs is based on the information provided in the document
"Uniform Resource Identifiers (URI): Generic Syntax" of "The Internet Society",
that can be found at http://www.ietf.org/rfc/rfc2396.txt.

Also interesting is http://www.freesoft.org/CIE/RFC/1738/33.htm which describes
general URL syntax and major protocols.

these protocols are recognized:
-------------------------------
http://
https://
ftp://
mailto:
news: or news://
nntp://
telnet://
gopher://
prospero://
wais://

as well as commonly used shorthands:
------------------------------------
someone@somewhere.org
www.host.org
}

{$IFNDEF QSYNHIGHLIGHTERURI}
unit SynHighlighterURI;
{$ENDIF}

{$I SynEdit.inc}

interface

uses
{$IFDEF SYN_CLX}
  QGraphics,
  QSynEditTypes,
  QSynEditHighlighter,
{$ELSE}
  Graphics,
  SynEditTypes,
  SynEditHighlighter,
{$ENDIF}
  SysUtils,
  Classes;

type
  TtkTokenKind = (tkNull, tkSpace, tkFtpLink, tkGopherLink,
    tkHttpLink, tkHttpsLink, tkMailtoLink, tkNewsLink, tkNntpLink,
    tkProsperoLink, tkTelnetLink, tkWaisLink, tkWebLink, tkUnknown, tkNullChar);

  TProcTableProc = procedure of object;

  PIdentFuncTableFunc = ^TIdentFuncTableFunc;
  TIdentFuncTableFunc = function: TtkTokenKind of object;

  TAlreadyVisitedURIFunc = function (URI: string): Boolean of object;

  TSynURISyn = class(TSynCustomHighlighter)
  private
    fLine: PChar;
    fLineNumber: Integer;
    fLineStr: string;
    fMayBeProtocol: PChar;
    fProcTable: array[#0..#255] of TProcTableProc;
    Run: LongInt;
    fStringLen: Integer;
    FTokenID: TtkTokenKind;
    fTokenPos: Integer;
    fIdentFuncTable: array[0..97] of TIdentFuncTableFunc;
    fIdentifierAttri: TSynHighlighterAttributes;
    fSpaceAttri: TSynHighlighterAttributes;
    fURIAttri: TSynHighlighterAttributes;
    fVisitedURIAttri: TSynHighlighterAttributes;
    FAlreadyVisitedURI: TAlreadyVisitedURIFunc;

    function KeyComp(const Key: string): Boolean;
    function KeyHash(ToHash: PChar): Integer;
    procedure InitIdent;
    procedure MakeMethodTables;

    procedure CRProc;
    procedure LFProc;
    procedure NullProc;
    procedure ProtocolProc;
    procedure SpaceProc;
    procedure UnknownProc;

    function AltFunc: TtkTokenKind;
    function FtpFunc: TtkTokenKind;
    function GopherFunc: TtkTokenKind;
    function HttpFunc: TtkTokenKind;
    function HttpsFunc: TtkTokenKind;
    function MailtoFunc: TtkTokenKind;
    function NewsFunc: TtkTokenKind;
    function NntpFunc: TtkTokenKind;
    function ProsperoFunc: TtkTokenKind;
    function TelnetFunc: TtkTokenKind;
    function WaisFunc: TtkTokenKind;
    function WebFunc: TtkTokenKind;

    function IsValidEmailAddress: Boolean;
    function IsValidURI: Boolean;
    function IsValidWebLink: Boolean;

    procedure SetURIAttri(const Value: TSynHighlighterAttributes);
    procedure SetVisitedURIAttri(const Value: TSynHighlighterAttributes);
  protected
    function GetIdentChars: TSynIdentChars; override;
    function GetSampleSource: string; override;
    function IsFilterStored: Boolean; override;
    procedure SetAlreadyVisitedURIFunc(Value: TAlreadyVisitedURIFunc);
  public
    class function GetLanguageName: string; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes;
      override;
    function GetEol: Boolean; override;
    function GetTokenID: TtkTokenKind;
    procedure SetLine(NewValue: string; LineNumber: Integer); override;
    function GetToken: string; override;
    function GetTokenAttribute: TSynHighlighterAttributes; override;
    function GetTokenKind: Integer; override;
    function GetTokenPos: Integer; override;
    procedure Next; override;
  published
    property URIAttri: TSynHighlighterAttributes read fURIAttri write SetURIAttri;
    property VisitedURIAttri: TSynHighlighterAttributes read fVisitedURIAttri
      write SetVisitedURIAttri;
  end;

const
  SYN_ATTR_URI = 6;
  SYN_ATTR_VISITEDURI = 7;

implementation

uses
{$IFDEF SYN_CLX}
  QSynEditStrConst;
{$ELSE}
  SynEditStrConst;
{$ENDIF}

const
  AlphaNum = ['0'..'9', 'A'..'Z', 'a'..'z'];
  Mark = ['-', '_', '.', '!', '~', '*', '''', '(' , ')'];
  Unreserved = Mark + AlphaNum;
  Reserved = [';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '%', '#'];
  URIChars = Reserved + Unreserved;
  NeverAtEnd = Mark - [''''] + Reserved - ['/', '$'];
  URIBreakChars = [#0..#255] - URIChars - [#0..#32];
  EMailAddressChars = ['.', '_', '-', '@'] + AlphaNum;
  NeverAtEMailAddressEnd = ['.', '@'];

var
  HashTable: array[#0..#255] of Integer;

procedure MakeHashTable;
var
  c: Char;
  u: Byte;
begin
  FillChar(HashTable, sizeof(HashTable), 0);

  for c := 'A' to 'Z' do
  begin
    u := Ord(UpCase(c));
    HashTable[c] := (u * u - 64) div 771;
  end;

  for c := 'a' to 'z' do
  begin
    u := Ord(UpCase(c));
    HashTable[c] := (u * u - 64) div 771;
  end;

  HashTable[':'] := HashTable['Z'] + 1;
  HashTable['/'] := HashTable['Z'] + 2;
end;

procedure TSynURISyn.InitIdent;
var
  I: Integer;
  pF: PIdentFuncTableFunc;
begin
  pF := PIdentFuncTableFunc(@fIdentFuncTable);
  for I := Low(fIdentFuncTable) to High(fIdentFuncTable) do begin
    pF^ := AltFunc;
    Inc(pF);
  end;

  fIdentFuncTable[27] := WebFunc;
  fIdentFuncTable[41] := NewsFunc;
  fIdentFuncTable[53] := MailtoFunc;
  fIdentFuncTable[58] := FtpFunc;
  fIdentFuncTable[63] := WaisFunc;
  fIdentFuncTable[65] := NewsFunc;
  fIdentFuncTable[66] := NntpFunc;
  fIdentFuncTable[67] := HttpFunc;
  fIdentFuncTable[77] := GopherFunc;
  fIdentFuncTable[79] := TelnetFunc;
  fIdentFuncTable[75] := HttpsFunc;
  fIdentFuncTable[97] := ProsperoFunc;
end;

function TSynURISyn.KeyComp(const Key: string): Boolean;
var
  I: Integer;
begin
  Result := True;
  for I := 1 to fStringLen do
    if HashTable[fMayBeProtocol[I - 1]] <> HashTable[Key[I]] then
    begin
      Result := False;
      break;
    end;
end;

function TSynURISyn.KeyHash(ToHash: PChar): Integer;
begin
  Result := 0;
  while ToHash^ in ['A'..'Z', 'a'..'z'] do
  begin
    inc(Result, HashTable[ToHash^]);
    inc(ToHash);
  end;

  if ToHash^ = ':' then
  begin
    inc(Result, HashTable[ToHash^]);
    inc(ToHash);

    if ToHash^ = '/' then
    begin
      inc(Result, HashTable[ToHash^]);
      inc(ToHash);

      if ToHash^ = '/' then
      begin
        inc(Result, HashTable[ToHash^]);
        inc(ToHash);
      end;
    end;
  end;
  fStringLen := ToHash - fMayBeProtocol;
end;

function TSynURISyn.AltFunc: TtkTokenKind;
begin
  Result := tkUnknown;
end;

procedure TSynURISyn.MakeMethodTables;
var
  I: Char;
begin
  for I := #0 to #255 do
    case I of
      #13: fProcTable[I] := CRProc;
      #10: fProcTable[I] := LFProc;
      #0: fProcTable[I] := NullProc;
      #1..#9, #11, #12, #14..#32: fProcTable[I] := SpaceProc;
      'A'..'Z', 'a'..'z': fProcTable[I] := ProtocolProc;
    else
      fProcTable[I] := UnknownProc;
    end;
end;

constructor TSynURISyn.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fSpaceAttri := TSynHighlighterAttributes.Create(SYNS_AttrSpace);
  fIdentifierAttri := TSynHighlighterAttributes.Create(SYNS_AttrIdentifier);

  fURIAttri := TSynHighlighterAttributes.Create(SYNS_AttrURI);
  fURIAttri.Foreground := clBlue;
  fURIAttri.Style := [fsUnderline];
  AddAttribute(fURIAttri);

  fVisitedURIAttri := TSynHighlighterAttributes.Create(SYNS_AttrVisitedURI);
  fVisitedURIAttri.Foreground := clPurple;
  fVisitedURIAttri.Style := [fsUnderline];
  AddAttribute(fVisitedURIAttri);

  SetAttributesOnChange(DefHighlightChange);
  InitIdent;
  MakeMethodTables;
  fDefaultFilter := SYNS_FilterURI;
end;

destructor TSynURISyn.Destroy; 
begin
  inherited;
  // the other attributes are automatically freed because of AddAttribute()
  fSpaceAttri.Free;
  fIdentifierAttri.Free;
end;

procedure TSynURISyn.SetLine(NewValue: string; LineNumber: Integer);
begin
  fLineStr := NewValue;
  fLine := PChar(NewValue);
  Run := 0;
  fLineNumber := LineNumber;
  Next;
end;

procedure TSynURISyn.CRProc;
begin
  fTokenID := tkSpace;
  inc(Run);
  if fLine[Run] = #10 then
    Inc(Run);
end;

procedure TSynURISyn.LFProc;
begin
  fTokenID := tkSpace;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -