Changeset 247

Show
Ignore:
Timestamp:
02/27/2008 11:27:58 (9 months ago)
Author:
jgarber
Message:

Scan for link aliases and insert the href where the aliases are used.

Location:
branches/superredcloth/ext/superredcloth_scan
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • branches/superredcloth/ext/superredcloth_scan/superredcloth.h

    r244 r247  
    1010void rb_str_cat_escaped(VALUE str, char *ts, char *te); 
    1111void rb_str_cat_escaped_for_preformatted(VALUE str, char *ts, char *te); 
    12 VALUE superredcloth_inline(VALUE, char *, char *); 
    13 VALUE superredcloth_inline2(VALUE, VALUE); 
    14 VALUE superredcloth_transform(VALUE, char *, char *); 
     12VALUE superredcloth_inline(VALUE, char *, char *, VALUE); 
     13VALUE superredcloth_inline2(VALUE, VALUE, VALUE); 
     14VALUE superredcloth_transform(VALUE, char *, char *, VALUE); 
    1515VALUE superredcloth_transform2(VALUE, VALUE); 
    1616void red_inc(VALUE, VALUE); 
    17 VALUE red_block(VALUE, VALUE, ID); 
    18 VALUE red_pass2(VALUE, VALUE, VALUE, VALUE); 
    19 VALUE red_pass(VALUE, VALUE, VALUE, ID); 
     17VALUE red_block(VALUE, VALUE, ID, VALUE); 
     18VALUE red_blockcode(VALUE, VALUE, VALUE); 
     19VALUE red_pass2(VALUE, VALUE, VALUE, VALUE, VALUE); 
     20VALUE red_pass(VALUE, VALUE, VALUE, ID, VALUE); 
    2021VALUE red_pass_code(VALUE, VALUE, VALUE, ID); 
    2122 
     
    2526#define INLINE(H, T)   rb_str_append(H, rb_funcall(rb_formatter, rb_intern(#T), 1, regs)) 
    2627#define DONE(H)        rb_str_append(html, H); CLEAR(H); regs = rb_hash_new() 
    27 #define PASS(H, A, T)  rb_str_append(H, red_pass(rb_formatter, regs, ID2SYM(rb_intern(#A)), rb_intern(#T))) 
    28 #define PASS2(H, A, T) rb_str_append(H, red_pass2(rb_formatter, regs, ID2SYM(rb_intern(#A)), ID2SYM(rb_intern(#T)))) 
     28#define PASS(H, A, T)  rb_str_append(H, red_pass(rb_formatter, regs, ID2SYM(rb_intern(#A)), rb_intern(#T), refs)) 
     29#define PASS2(H, A, T) rb_str_append(H, red_pass2(rb_formatter, regs, ID2SYM(rb_intern(#A)), ID2SYM(rb_intern(#T)), refs)) 
    2930#define PASS_CODE(H, A, T) rb_str_append(H, red_pass_code(rb_formatter, regs, ID2SYM(rb_intern(#A)), rb_intern(#T))) 
    3031#define ADD_BLOCK() \ 
    31   rb_str_append(html, red_block(rb_formatter, regs, block)); \ 
     32  rb_str_append(html, red_block(rb_formatter, regs, block, refs)); \ 
    3233  extend = Qnil; \ 
    3334  CLEAR(block); \ 
    3435  regs = rb_hash_new() 
    35 #define ADD_EXTENDED_BLOCK()    rb_str_append(html, red_block(rb_formatter, regs, block)); CLEAR(block); 
     36#define ADD_EXTENDED_BLOCK()    rb_str_append(html, red_block(rb_formatter, regs, block, refs)); CLEAR(block); 
    3637#define ADD_BLOCKCODE()    rb_str_append(html, red_blockcode(rb_formatter, regs, block)); CLEAR(block); regs = rb_hash_new() 
    3738#define ADD_EXTENDED_BLOCKCODE()    rb_str_append(html, red_blockcode(rb_formatter, regs, block)); CLEAR(block); 
     
    6667  } \ 
    6768  STORE(T); \ 
    68   p--; 
     69  p--; \ 
     70  if ( !NIL_P(refs) && rb_funcall(refs, rb_intern("has_key?"), 1, rb_hash_aref(regs, ID2SYM(rb_intern(#T)))) ) { \ 
     71    rb_hash_aset(regs, ID2SYM(rb_intern(#T)), rb_hash_aref(refs, rb_hash_aref(regs, ID2SYM(rb_intern(#T))))); \ 
     72  } 
    6973#define LIST_ITEM() \ 
    7074    int aint = 0; \ 
  • branches/superredcloth/ext/superredcloth_scan/superredcloth_common.rl

    r244 r247  
    6565  html_comment = "<!--" (default+) :> "-->"; 
    6666 
     67  # URI tokens (lifted from Mongrel) 
     68  CTL = (cntrl | 127); 
     69  safe = ("$" | "-" | "_" | "."); 
     70  extra = ("!" | "*" | "'" | "(" | ")" | "," | "#"); 
     71  reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+"); 
     72  unsafe = (CTL | " " | "\"" | "%" | "<" | ">"); 
     73  national = any -- (alpha | digit | reserved | extra | safe | unsafe); 
     74  unreserved = (alpha | digit | safe | extra | national); 
     75  escape = ("%" xdigit xdigit); 
     76  uchar = (unreserved | escape); 
     77  pchar = (uchar | ":" | "@" | "&" | "=" | "+"); 
     78  scheme = ( alpha | digit | "+" | "-" | "." )+ ; 
     79  absolute_uri = (scheme ":" (uchar | reserved )*); 
     80  safepath = (pchar* (alpha | digit | safe) pchar*) ; 
     81  path = (safepath ( "/" pchar* )*) ; 
     82  query = ( uchar | reserved )* ; 
     83  param = ( pchar | "/" )* ; 
     84  params = (param ( ";" param )*) ; 
     85  rel_path = (path (";" params)?) ("?" query)?; 
     86  absolute_path = ("/"+ rel_path?); 
     87  target = ("#" pchar*) ; 
     88  uri = (target | absolute_uri | absolute_path | rel_path) ; 
     89 
    6790}%%; 
  • branches/superredcloth/ext/superredcloth_scan/superredcloth_inline.rl

    r244 r247  
    1414  machine superredcloth_inline; 
    1515  include superredcloth_common "ext/superredcloth_scan/superredcloth_common.rl"; 
    16  
    17   # URI tokens (lifted from Mongrel) 
    18   CTL = (cntrl | 127); 
    19   safe = ("$" | "-" | "_" | "."); 
    20   extra = ("!" | "*" | "'" | "(" | ")" | "," | "#"); 
    21   reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+"); 
    22   unsafe = (CTL | " " | "\"" | "%" | "<" | ">"); 
    23   national = any -- (alpha | digit | reserved | extra | safe | unsafe); 
    24   unreserved = (alpha | digit | safe | extra | national); 
    25   escape = ("%" xdigit xdigit); 
    26   uchar = (unreserved | escape); 
    27   pchar = (uchar | ":" | "@" | "&" | "=" | "+"); 
    28   scheme = ( alpha | digit | "+" | "-" | "." )+ ; 
    29   absolute_uri = (scheme ":" (uchar | reserved )*); 
    30   safepath = (pchar* (alpha | digit | safe) pchar*) ; 
    31   path = (safepath ( "/" pchar* )*) ; 
    32   query = ( uchar | reserved )* ; 
    33   param = ( pchar | "/" )* ; 
    34   params = (param ( ";" param )*) ; 
    35   rel_path = (path (";" params)?) ("?" query)?; 
    36   absolute_path = ("/"+ rel_path?); 
    37   target = ("#" pchar*) ; 
    38   uri = (target | absolute_uri | absolute_path | rel_path) ; 
    3916 
    4017  # common 
     
    150127 
    151128VALUE 
    152 red_pass(VALUE rb_formatter, VALUE regs, VALUE ref, ID meth) 
     129red_pass(VALUE rb_formatter, VALUE regs, VALUE ref, ID meth, VALUE refs) 
    153130{ 
    154131  VALUE txt = rb_hash_aref(regs, ref); 
    155   if (!NIL_P(txt)) rb_hash_aset(regs, ref, superredcloth_inline2(rb_formatter, txt)); 
     132  if (!NIL_P(txt)) rb_hash_aset(regs, ref, superredcloth_inline2(rb_formatter, txt, refs)); 
    156133  return rb_funcall(rb_formatter, meth, 1, regs); 
    157134} 
     
    170147 
    171148VALUE 
    172 red_pass2(VALUE rb_formatter, VALUE regs, VALUE ref, VALUE btype) 
     149red_pass2(VALUE rb_formatter, VALUE regs, VALUE ref, VALUE btype, VALUE refs) 
    173150{ 
    174151  btype = rb_hash_aref(regs, btype); 
    175152  StringValue(btype); 
    176   return red_pass(rb_formatter, regs, ref, rb_intern(RSTRING(btype)->ptr)); 
    177 } 
    178  
    179 VALUE 
    180 red_block(VALUE rb_formatter, VALUE regs, VALUE block) 
     153  return red_pass(rb_formatter, regs, ref, rb_intern(RSTRING(btype)->ptr), refs); 
     154} 
     155 
     156VALUE 
     157red_block(VALUE rb_formatter, VALUE regs, VALUE block, VALUE refs) 
    181158{ 
    182159  VALUE btype = rb_hash_aref(regs, ID2SYM(rb_intern("type"))); 
     
    184161  if ((RSTRING(block)->len > 0) && !NIL_P(btype)) 
    185162  { 
    186     rb_hash_aset(regs, ID2SYM(rb_intern("text")), superredcloth_inline2(rb_formatter, block)); 
     163    rb_hash_aset(regs, ID2SYM(rb_intern("text")), superredcloth_inline2(rb_formatter, block, refs)); 
    187164    block = rb_funcall(rb_formatter, rb_intern(RSTRING(btype)->ptr), 1, regs); 
    188165  } 
     
    213190 
    214191VALUE 
    215 superredcloth_inline(rb_formatter, p, pe) 
     192superredcloth_inline(rb_formatter, p, pe, refs) 
    216193  VALUE rb_formatter; 
    217194  char *p, *pe; 
     195  VALUE refs; 
    218196{ 
    219197  int cs, act; 
     
    295273 
    296274VALUE 
    297 superredcloth_inline2(formatter, str) 
    298   VALUE formatter, str; 
     275superredcloth_inline2(formatter, str, refs) 
     276  VALUE formatter, str, refs; 
    299277{ 
    300278  StringValue(str); 
    301   return superredcloth_inline(formatter, RSTRING(str)->ptr, RSTRING(str)->ptr + RSTRING(str)->len + 1); 
    302 } 
     279  return superredcloth_inline(formatter, RSTRING(str)->ptr, RSTRING(str)->ptr + RSTRING(str)->len + 1, refs); 
     280} 
  • branches/superredcloth/ext/superredcloth_scan/superredcloth_scan.rl

    r244 r247  
    4343  list_start  = ( ( ul | ol )+ N A C :> " "+ ) >{nest = 0;} ; 
    4444  blank_line = CRLF; 
     45  link_alias = ( "[" >{ ASET(type, ignore) } %A phrase %T "]" %A uri ) ; 
    4546   
    4647  # html blocks 
     
    139140    list_start      { list_layout = rb_ary_new(); LIST_ITEM(); fgoto list; }; 
    140141    table           { INLINE(table, table_close); DONE(table); fgoto block; }; 
     142    link_alias      { STORE_URL(href); rb_hash_aset(refs_found, rb_hash_aref(regs, ID2SYM(rb_intern("text"))), rb_hash_aref(regs, ID2SYM(rb_intern("href")))); DONE(block); }; 
    141143    blank_line => cat; 
    142144    default 
     
    155157 
    156158VALUE 
    157 superredcloth_transform(rb_formatter, p, pe) 
     159superredcloth_transform(rb_formatter, p, pe, refs) 
    158160  VALUE rb_formatter; 
    159161  char *p, *pe; 
    160 { 
     162  VALUE refs; 
     163{ 
     164  char *orig_p = p, *orig_pe = pe; 
    161165  int cs, act, nest; 
    162166  char *ts = NULL, *te = NULL, *reg = NULL, *eof = NULL; 
     
    172176  VALUE extend = Qnil; 
    173177  char listm[10] = ""; 
     178  VALUE refs_found = rb_hash_new(); 
    174179 
    175180  %% write init; 
     
    182187  } 
    183188 
    184   return html; 
     189  if ( NIL_P(refs) && rb_funcall(refs_found, rb_intern("empty?"), 0) == Qfalse ) { 
     190    return superredcloth_transform(rb_formatter, orig_p, orig_pe, refs_found); 
     191  } else { 
     192    return html; 
     193  } 
    185194} 
    186195 
     
    191200  rb_str_cat2(str, "\n"); 
    192201  StringValue(str); 
    193   return superredcloth_transform(formatter, RSTRING(str)->ptr, RSTRING(str)->ptr + RSTRING(str)->len + 1); 
     202  return superredcloth_transform(formatter, RSTRING(str)->ptr, RSTRING(str)->ptr + RSTRING(str)->len + 1, Qnil); 
    194203} 
    195204