Quote Fortune Macro


인용문들이 저장된 페이지에서 임의의 한 인용문을 선택해서 보여주는 매크로
QuoteFortune("<페이지이름>"[,"<정규식>"])

<정규식>을 생략하면 해당 페이지에서 {{| |}} 로 표시된 부분들 중에 하나를 골라준다. 총알(*)로 시작하는 인용문들의 집합에서 하나를 고를 경우는 다음 정규식을 사용할 수 있다: "(?m)^\s*\*\s*(.*)$"

e.g.

  • 니체는 당신의 문제에 대해 "[[QuoteFortune("니체어록")]]"라고 말해줍니다.
  • 주역의 64괘 중 당신이 뽑은 괘는 "[[QuoteFortune("주역64괘")]]"입니다.
  • 축하드립니다. [[QuoteFortune("직원목록","(?m)^\s*\*\s*(.*)$")]]님께 피자와 순대를 사오실 수 있는 특권을 드립니다.

#macro/QuoteFortune.py 
 
""" 
    MoinMoin - QuoteFortune Macro 
 
    Copyright (c) 2002 by Changjune Kim <juneaftn@orgio.net> 
    All rights reserved 
 
Usage: 
   [[QuoteFortune("AnyPageName","AnyRegEx")]] or 
   [[QuoteFortune("AnyPageName")]] (in which case the default pattern is used) 
 
   if you need to put a double quote(") in the regex, you should put it escaped as \" 
 
Notes: 
   Since whrandom module is not thread-safe and uses the system clock as the seed, 
   which is somewhat coarse-grained, it might be quite possible that the clients 
   might get the same choice for a short time period(apprx. 4ms theoretically, but it 
   might vary according to the system's time call precision). 
""" 
from MoinMoin import wikiutil, config 
from MoinMoin.Page import Page 
from MoinMoin.parser.wiki import Parser 
import re, whrandom, sys 
from cStringIO import StringIO 
 
p=r'''(?x) 
(?P<quote>['"])(?P<pagename>[^'"]+)(?P=quote) 
( 
  (?P<spacer>\s*,\s*) 
  (?P=quote)(?P<regex>[^'"]+)(?P=quote) 
)?''' 
 
DEFAULT_PATTERN=r'(?s){{\|(.*?)\|}}' 
 
def execute(macro,args,args_p=re.compile(p)): 
    m=args_p.match(args) 
    if not m: 
        return "Wrong arguments" 
    m=m.groupdict() 
    if not m['regex']: 
        m['regex']=DEFAULT_PATTERN 
    all_pages=wikiutil.getPageList(config.text_dir) 
    if m['pagename'] not in all_pages: 
        return "No such page exists" 
    quoteString=getaQuoteFromaWikiPage(m['pagename'],m['regex']) 
    parser=Parser(quoteString) 
    stdout=sys.stdout 
    sys.stdout=StringIO() 
    parser.format(macro.formatter, None) 
    result=sys.stdout.getvalue() 
    sys.stdout=stdout 
    return result 
 
def getQuoteList(aString,aPattern):  
    found=re.findall(aPattern,aString)  
    truncated=[]  
    for each in found:  
        if each.startswith('\n'):  
            each=each[1:]  
        if each.endswith('\n'):  
            each=each[:-1]  
        truncated.append(each)  
    return truncated  
  
def getaQuote(aString,aPattern):  
    return whrandom.choice(getQuoteList(aString,aPattern) ) 
  
def getaQuoteFromaWikiPage(aPageName,aPattern):  
    return getaQuote(Page(aPageName).get_raw_body(),aPattern) 

매크로분류