text:
package org.apache.flink.table.runtime.operators.join.temporal;

import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.VoidNamespace;
import org.apache.flink.runtime.state.VoidNamespaceSerializer;
import org.apache.flink.streaming.api.operators.InternalTimer;
import org.apache.flink.streaming.api.operators.InternalTimerService;
import org.apache.flink.streaming.api.operators.TimestampedCollector;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
import org.apache.flink.table.data.JoinedRowData;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.data.util.RowDataUtil;
import org.apache.flink.table.runtime.generated.GeneratedJoinCondition;
import org.apache.flink.table.runtime.generated.JoinCondition;
import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
import org.apache.flink.types.RowKind;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;

/**
 * This operator works by keeping on the state collection of probe and build records to process
 * on next watermark. The idea is that between watermarks we are collecting those elements
 * and once we are sure that there will be no updates we emit the correct result and clean up the
 * state.
 *
 *
Cleaning up the state drops all of the "old" values from the probe side, where "old" is defined
 * as older then the current watermark. Build side is also cleaned up in the similar fashion,
 * however we always keep at least one record - the latest one - even if it's past the last
 * watermark.
 *
 *
One more trick is how the emitting results and cleaning up is triggered. It is achieved
 * by registering timers for the keys. We could register a timer for every probe and build
 * side element's event time (when watermark exceeds this timer, that's when we are emitting and/or
 * cleaning up the state). However this would cause huge number of registered timers. For example
 * with following evenTimes of probe records accumulated: {1, 2, 5, 8, 9}, if we
 * had received Watermark(10), it would trigger 5 separate timers for the same key. To avoid that
 * we always keep only one single registered timer for any given key, registered for the minimal
 * value. Upon triggering it, we process all records with event times older then or equal to
 * currentWatermark.
 */
public class TemporalRowTimeJoinOperator
	extends BaseTwoInputStreamOperatorWithStateRetention {

	private static final long serialVersionUID = 6642514795175288193L;

	private static final String NEXT_LEFT_INDEX_STATE_NAME = "next-index";
	private static final String LEFT_STATE_NAME = "left";
	private static final String RIGHT_STATE_NAME = "right";
	private static final String REGISTERED_TIMER_STATE_NAME = "timer";
	private static final String TIMERS_STATE_NAME = "timers";

	private final InternalTypeInfo<RowData> leftType;
	private final InternalTypeInfo<RowData> rightType;
	private final GeneratedJoinCondition generatedJoinCondition;
	private final int leftTimeAttribute;
	private final int rightTimeAttribute;

	private final RowtimeComparator rightRowtimeComparator;

	/**
	 * Incremental index generator for {@link #leftState}'s keys.
	 */
	private transient ValueState<Long> nextLeftIndex;

	/**
	 * Mapping from artificial row index (generated by `nextLeftIndex`) into the left side `Row`.
	 * We can not use List to accumulate Rows, because we need efficient deletes of the oldest rows.
	 *
	 *
TODO: this could be OrderedMultiMap[Jlong, Row] indexed by row's timestamp, to avoid
	 * full map traversals (if we have lots of rows on the state that exceed `currentWatermark`).
	 */
	private transient MapState<Long, RowData> leftState;

	/**
	 * Mapping from timestamp to right side `Row`.
	 *
	 *
TODO: having `rightState` as an OrderedMapState would allow us to avoid sorting cost
	 * once per watermark
	 */
	private transient MapState<Long, RowData> rightState;

	// Long for correct handling of default null
	private transient ValueState<Long> registeredTimer;
	private transient TimestampedCollector<RowData> collector;
	private transient InternalTimerService<VoidNamespace> timerService;

	private transient JoinCondition joinCondition;
	private transient JoinedRowData outRow;

	public TemporalRowTimeJoinOperator(
			InternalTypeInfo<RowData> leftType,
			InternalTypeInfo<RowData> rightType,
			GeneratedJoinCondition generatedJoinCondition,
			int leftTimeAttribute,
			int rightTimeAttribute,
			long minRetentionTime,
			long maxRetentionTime) {
		super(minRetentionTime, maxRetentionTime);
		this.leftType = leftType;
		this.rightType = rightType;
		this.generatedJoinCondition = generatedJoinCondition;
		this.leftTimeAttribute = leftTimeAttribute;
		this.rightTimeAttribute = rightTimeAttribute;
		this.rightRowtimeComparator = new RowtimeComparator(rightTimeAttribute);
	}

	@Override
	public void open() throws Exception {
		joinCondition = generatedJoinCondition.newInstance(getRuntimeContext().getUserCodeClassLoader());
		joinCondition.setRuntimeContext(getRuntimeContext());
		joinCondition.open(new Configuration());

		nextLeftIndex = getRuntimeContext().getState(
			new ValueStateDescriptor<>(NEXT_LEFT_INDEX_STATE_NAME, Types.LONG));
		leftState = getRuntimeContext().getMapState(
			new MapStateDescriptor<>(LEFT_STATE_NAME, Types.LONG, leftType));
		rightState = getRuntimeContext().getMapState(
			new MapStateDescriptor<>(RIGHT_STATE_NAME, Types.LONG, rightType));
		registeredTimer = getRuntimeContext().getState(
			new ValueStateDescriptor<>(REGISTERED_TIMER_STATE_NAME, Types.LONG));

		timerService = getInternalTimerService(
			TIMERS_STATE_NAME, VoidNamespaceSerializer.INSTANCE, this);
		collector = new TimestampedCollector<>(output);
		outRow = new JoinedRowData();
		// all the output records should be INSERT only,
		// because current temporal join only supports INSERT only left stream
		outRow.setRowKind(RowKind.INSERT);
	}

	@Override
	public void processElement1(StreamRecord<RowData> element) throws Exception {
		RowData row = element.getValue();
		checkNotRetraction(row);

		leftState.put(getNextLeftIndex(), row);
		registerSmallestTimer(getLeftTime(row)); // Timer to emit and clean up the state

		registerProcessingCleanupTimer();
	}

	@Override
	public void processElement2(StreamRecord<RowData> element) throws Exception {
		RowData row = element.getValue();
		checkNotRetraction(row);

		long rowTime = getRightTime(row);
		rightState.put(rowTime, row);
		registerSmallestTimer(rowTime); // Timer to clean up the state

		registerProcessingCleanupTimer();
	}

	@Override
	public void onEventTime(InternalTimer<Object, VoidNamespace> timer) throws Exception {
		registeredTimer.clear();
		long lastUnprocessedTime = emitResultAndCleanUpState(timerService.currentWatermark());
		if (lastUnprocessedTime < Long.MAX_VALUE) {
			registerTimer(lastUnprocessedTime);
		}

		// if we have more state at any side, then update the timer, else clean it up.
		if (stateCleaningEnabled) {
			if (lastUnprocessedTime < Long.MAX_VALUE || !rightState.isEmpty()) {
				registerProcessingCleanupTimer();
			} else {
				cleanupLastTimer();
			}
		}
	}

	@Override
	public void close() throws Exception {
		if (joinCondition != null) {
			joinCondition.close();
		}
	}

	/**
	 * @return a row time of the oldest unprocessed probe record or Long.MaxValue, if all records
	 *         have been processed.
	 */
	private long emitResultAndCleanUpState(long timerTimestamp) throws Exception {
		List<RowData> rightRowsSorted = getRightRowSorted(rightRowtimeComparator);
		long lastUnprocessedTime = Long.MAX_VALUE;

		Iterator<Map.Entry<Long, RowData>> leftIterator = leftState.entries().iterator();
		while (leftIterator.hasNext()) {
			Map.Entry<Long, RowData> entry = leftIterator.next();
			RowData leftRow = entry.getValue();
			long leftTime = getLeftTime(leftRow);

			if (leftTime <= timerTimestamp) {
				Optional<RowData> rightRow = latestRightRowToJoin(rightRowsSorted, leftTime);
				if (rightRow.isPresent()) {
					if (joinCondition.apply(leftRow, rightRow.get())) {
						outRow.replace(leftRow, rightRow.get());
						collector.collect(outRow);
					}
				}
				leftIterator.remove();
			} else {
				lastUnprocessedTime = Math.min(lastUnprocessedTime, leftTime);
			}
		}

		cleanupState(timerTimestamp, rightRowsSorted);
		return lastUnprocessedTime;
	}

	/**
	 * Removes all right entries older then the watermark, except the latest one. For example with:
	 * rightState = [1, 5, 9]
	 * and
	 * watermark = 6
	 * we can not remove "5" from rightState, because left elements with rowtime of 7 or 8 could
	 * be joined with it later
	 */
	private void cleanupState(long timerTimestamp, List<RowData> rightRowsSorted) throws Exception {
		int i = 0;
		int indexToKeep = firstIndexToKeep(timerTimestamp, rightRowsSorted);
		while (i < indexToKeep) {
			long rightTime = getRightTime(rightRowsSorted.get(i));
			rightState.remove(rightTime);
			i += 1;
		}
	}

	/**
	 * The method to be called when a cleanup timer fires.
	 *
	 * @param time The timestamp of the fired timer.
	 */
	@Override
	public void cleanupState(long time) {
		leftState.clear();
		rightState.clear();
	}

	private int firstIndexToKeep(long timerTimestamp, List<RowData> rightRowsSorted) {
		int firstIndexNewerThenTimer =
			indexOfFirstElementNewerThanTimer(timerTimestamp, rightRowsSorted);

		if (firstIndexNewerThenTimer < 0) {
			return rightRowsSorted.size() - 1;
		}
		else {
			return firstIndexNewerThenTimer - 1;
		}
	}

	private int indexOfFirstElementNewerThanTimer(long timerTimestamp, List<RowData> list) {
		ListIterator<RowData> iter = list.listIterator();
		while (iter.hasNext()) {
			if (getRightTime(iter.next()) > timerTimestamp) {
				return iter.previousIndex();
			}
		}
		return -1;
	}

	/**
	 * Binary search {@code rightRowsSorted} to find the latest right row to join with {@code leftTime}.
	 * Latest means a right row with largest time that is still smaller or equal to {@code leftTime}.
	 *
	 * @return found element or {@code Optional.empty} If such row was not found (either {@code rightRowsSorted}
	 *         is empty or all {@code rightRowsSorted} are are newer).
	 */
	private Optional<RowData> latestRightRowToJoin(List<RowData> rightRowsSorted, long leftTime) {
		return latestRightRowToJoin(rightRowsSorted, 0, rightRowsSorted.size() - 1, leftTime);
	}

	private Optional<RowData> latestRightRowToJoin(
			List<RowData> rightRowsSorted,
			int low,
			int high,
			long leftTime) {
		if (low > high) {
			// exact value not found, we are returning largest from the values smaller then leftTime
			if (low - 1 < 0) {
				return Optional.empty();
			}
			else {
				return Optional.of(rightRowsSorted.get(low - 1));
			}
		} else {
			int mid = (low + high) >>> 1;
			RowData midRow = rightRowsSorted.get(mid);
			long midTime = getRightTime(midRow);
			int cmp = Long.compare(midTime, leftTime);
			if (cmp < 0) {
				return latestRightRowToJoin(rightRowsSorted, mid + 1, high, leftTime);
			}
			else if (cmp > 0) {
				return latestRightRowToJoin(rightRowsSorted, low, mid - 1, leftTime);
			}
			else {
				return Optional.of(midRow);
			}
		}
	}

	private void registerSmallestTimer(long timestamp) throws IOException {
		Long currentRegisteredTimer = registeredTimer.value();
		if (currentRegisteredTimer == null) {
			registerTimer(timestamp);
		} else if (currentRegisteredTimer > timestamp) {
			timerService.deleteEventTimeTimer(VoidNamespace.INSTANCE, currentRegisteredTimer);
			registerTimer(timestamp);
		}
	}

	private void registerTimer(long timestamp) throws IOException {
		registeredTimer.update(timestamp);
		timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, timestamp);
	}

	private List<RowData> getRightRowSorted(RowtimeComparator rowtimeComparator) throws Exception {
		List<RowData> rightRows = new ArrayList<>();
		for (RowData row : rightState.values()) {
			rightRows.add(row);
		}
		rightRows.sort(rowtimeComparator);
		return rightRows;
	}

	private long getNextLeftIndex() throws IOException {
		Long index = nextLeftIndex.value();
		if (index == null) {
			index = 0L;
		}
		nextLeftIndex.update(index + 1);
		return index;
	}

	private long getLeftTime(RowData leftRow) {
		return leftRow.getLong(leftTimeAttribute);
	}

	private long getRightTime(RowData rightRow) {
		return rightRow.getLong(rightTimeAttribute);
	}

	private void checkNotRetraction(RowData row) {
		if (RowDataUtil.isRetractMsg(row)) {
			String className = getClass().getSimpleName();
			throw new IllegalStateException(
				"Retractions are not supported by " + className +
					". If this can happen it should be validated during planning!");
		}
	}

	// ------------------------------------------------------------------------------------------

	private static class RowtimeComparator implements Comparator<RowData>, Serializable {

		private static final long serialVersionUID = 8160134014590716914L;

		private final int timeAttribute;

		private RowtimeComparator(int timeAttribute) {
			this.timeAttribute = timeAttribute;
		}

		@Override
		public int compare(RowData o1, RowData o2) {
			long o1Time = o1.getLong(timeAttribute);
			long o2Time = o2.getLong(timeAttribute);
			return Long.compare(o1Time, o2Time);
		}
	}
}


text:
#ifndef CPU_X86_MM_SEGMENTATION_H_
#define CPU_X86_MM_SEGMENTATION_H_

#include <stdint.h>

#define SEG_FLAG(lbl, val)                                                    \
  (((val) & (~0u >> (32 - SEG_WIDTH_##lbl))) << SEG_SHAMT_##lbl)

#define SEG_SET_FLAG(desc, lbl, val)                                          \
  (desc).flags = ((desc).flags & ~SEG_FLAG(lbl, ~0u)) | SEG_FLAG(lbl, val)

#define SEG_WIDTH_TYPE       4
#define SEG_SHAMT_TYPE       0
#define SEG_WIDTH_DESCTYPE   1
#define SEG_SHAMT_DESCTYPE   4
#define SEG_WIDTH_DPL        2
#define SEG_SHAMT_DPL        5
#define SEG_WIDTH_PRESENT    1
#define SEG_SHAMT_PRESENT    7
#define SEG_WIDTH_LIMIT_HI   4
#define SEG_SHAMT_LIMIT_HI   8
#define SEG_WIDTH_AVL        1
#define SEG_SHAMT_AVL        12
#define SEG_WIDTH_LONG_MODE  1
#define SEG_SHAMT_LONG_MODE  13
/* also used to indicate default operand and address size */
#define SEG_WIDTH_DIRECTION  1
#define SEG_SHAMT_DIRECTION  14
#define SEG_WIDTH_GRAN       1
#define SEG_SHAMT_GRAN       15

#define SEG_TYPE_DATA_RDONLY SEG_FLAG(TYPE, 0x00) /* Read only */
#define SEG_TYPE_DATA_RDWR   SEG_FLAG(TYPE, 0x02) /* Read/Write */
#define SEG_TYPE_CODE_EXRD   SEG_FLAG(TYPE, 0x0A) /* Execute/Read */
#define SEG_TYPE_CODE_EX     SEG_FLAG(TYPE, 0x08) /* Execute only */
#define SEG_TYPE_LDT         SEG_FLAG(TYPE, 0x02)
#define SEG_TYPE_TSS32_AVAIL SEG_FLAG(TYPE, 0x09)

#define SEG_DESCTYPE_SYS     SEG_FLAG(DESCTYPE, 0)
#define SEG_DESCTYPE_NSYS    SEG_FLAG(DESCTYPE, 1)

#define SEG_PRESENT          SEG_FLAG(PRESENT, 1)

#define SEG_DEFL_OPSZ_32BIT  SEG_FLAG(DIRECTION, 1)

#define SEG_GRAN_BYTE        SEG_FLAG(GRAN, 0)
#define SEG_GRAN_PAGE        SEG_FLAG(GRAN, 1)

/**
 * Maximum length of segment that can be regulated with a byte-granularity
 * segment limit.
 */
#define SEG_MAX_BYTE_GRAN_LEN (1 << 20)

/**
 * Segment descriptor.  See Intel Combined Manual,
 * Vol. 3, Section 3.4.5 for more details.
 */
typedef union segment_desc {
  struct {
    uint32_t lim_lo     : 16;
    uint32_t base_lo    : 16;
    uint32_t base_mid   : 8;
    uint32_t flags      : 16;
    uint32_t base_hi    : 8;
  };
  struct {
    uint32_t raw_lo, raw_hi;
  };
  uint64_t raw;
} segment_desc_t;

#define SEG_DESC_NOT_PRESENT 0

/* The next two functions are invoked by boot code, so they must always be
 * inlined to avoid being placed in a different address space than the initial,
 * flat address space.
 */
static inline void __attribute__((always_inline))
segment_desc_set_limit(segment_desc_t *c_this, uint32_t len)
{
  uint32_t limit = len - 1;

  SEG_SET_FLAG(*c_this, LIMIT_HI, limit >> 16); /* set limit bits 19:16 */
  c_this->lim_lo = limit; /* set limit bits 15:0 */
}
/**
 * \brief        Initialize a segment descriptor.
 * \param c_this Segment descriptor to be initialized.
 * \param base   Base address of region to be covered by segment descriptor.
 * \param len    Length to be specified by segment descriptor.  The units may
 *               be bytes or pages, depending on the flags.
 * \param flags  Flags to be added to the default flags: present, default
 *               operand size of 32 bits, and high limit bits.
 */
static inline void __attribute__((always_inline))
segment_desc_init(segment_desc_t *c_this,
                  uint32_t base, uint32_t len, uint16_t flags)
{
  c_this->raw = 0;

  /* Create the high 32 bit segment */
  c_this->base_mid = base >> 16; /* set base bits 23:16 */
  c_this->base_hi = base >> 24; /* set base bits 31:24 */

  /* Create the low 32 bit segment */
  c_this->base_lo = base; /* set base bits 15:0 */

  c_this->flags = SEG_FLAG(PRESENT, 1) | SEG_DEFL_OPSZ_32BIT | flags;

  /* This must be done after setting the other flags, or else it
   * would be partially overridden.
   */
  segment_desc_set_limit(c_this, len);
}
#endif /* CPU_X86_MM_SEGMENTATION_H_ */

redpajama_set_name: RedPajamaGithub


text:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    zero_vector<double> v (3);
    std::cout << v << std::endl;
}


text:
Home of the me.js project

More details to follow...


text:
import re
import urlparse
import urllib
import keyword
import warnings
import traceback
import unicodedata

import genshi.builder as bldr
from genshi.core import Stream, Markup

from core import (escape_char, esc_neg_look, fragmentize,
                  ImplicitList, AttrDict, MacroError)

BLOCK_ONLY_TAGS = ['h1','h2','h3','h4','h5','h6',
              'ul','ol','dl',
              'pre','hr','blockquote','address',
              'p','div','form','fieldset','table',
              'noscript']

BLOCK_TAGS = BLOCK_ONLY_TAGS + ['ins','del','script']

MACRO_NAME = r'(?P<name>[a-zA-Z][a-zA-Z0-9]*([-.][a-zA-Z0-9]+)*)'
"""allows any number of non-repeating hyphens or periods.
Underscore is not included because hyphen is"""

# use Genshi's HTMLSanitizer if possible (i.e., not on Google App Engine)
try:
    from genshi.filters import HTMLSanitizer
except:
    SAFE_SCHEMES = frozenset(['file', 'ftp', 'http', 'https', 'mailto', None])
    class HTMLSanitizer(object):
        def is_safe_uri(self,uri):
            if ':' not in uri:
                return True # This is a relative URI
            chars = [char for char in uri.split(':', 1)[0] if char.isalnum()]
            return ''.join(chars).lower() in SAFE_SCHEMES

sanitizer = HTMLSanitizer()

__docformat__ = 'restructuredtext en'

class WikiElement(object):

    """Baseclass for all wiki elements."""

    append_newline = False
    """Determines if newlines are appended to Element(s) during processing.
    Should only affect readability of source xml.
    """

    def __init__(self, tag, token, child_elements=None):
        """Constructor for WikiElement objects.

        Subclasses may have other keyword arguments.

        :parameters:
          tag
            The xhtml tag associated with the element.
          token
            The character string (or strings) that identifies the element
            in wiki markup.
          child_elements
            A list of wiki_elements that will be searched for in the body of the
            element.  The order of these elements matters, because if an element is
            found before the element that encloses it, the enclosing element will
            never be found.  In cases where this imposes limits (e.g, ``strong`` and
            ``em`` should be allowed to nest each other), place the conflicting
            elements in a sublist. The parser will then find which comes first.
        """
        self.tag = tag
        self.token = token
        if child_elements is None:
            child_elements = []
        self.child_elements = child_elements

    def _build(self,mo,element_store, environ):
        """Returns a genshi Element that has ``self.tag`` as the
        outermost tag.

        This methods if called exclusively by ``_process``

        :parameters:
          mo
            match object, usually the one returned by
            self.regexp.search(s)
        """
        return bldr.tag.__getattr__(self.tag)(fragmentize(mo.group(1),
                                                          self.child_elements,
                                                          element_store, environ))

    def re_string(self):
        """The regular expression pattern that is compiled into ``self.regexp``.

        The regular expression must consume the entire wiki element,
        including the tokens. For block elements, the newline on the last
        line must be consumed also. group(1) should normally be the
        entire string inside the tokens. If not, a custom ``_build``
        method will be needed.
        """
        pass

    def __repr__(self):
        return "<"+self.__class__.__name__ + " " + str(self.tag)+">"

    def _process(self, mos, text, wiki_elements,element_store, environ):
        """Returns genshi Fragments (Elements and text)

        This is mainly for block level markup. See InlineElement
        for the other method.
        """
        frags = []
        end = 0
        for mo in mos:
            if end != mo.start():
            # call again for leading text and extend the result list
                frags.extend(fragmentize(text[end:mo.start()],wiki_elements[1:],
                                         element_store, environ))
            # append the found wiki element to the result list
            built = self._build(mo,element_store, environ)
            if built is not None:
                frags.append(built)
            # make the source output easier to read
            if self.append_newline:
                frags.append('\n')
            end = mo.end()
        # call again for trailing text and extend the result list
        if end < len(text):
            if not isinstance(wiki_elements[0],(list,tuple)):
                wiki_elements = wiki_elements[1:]
            frags.extend(fragmentize(text[end:],wiki_elements,
                                         element_store, environ))

        return frags

class BlockElement(WikiElement):

    """Block elements inherit form this class

    Wiki elements wanting ``append_newline = True`` should use this
    as the base also.

    """

    append_newline = True

class InlineElement(WikiElement):

    r"""For finding generic inline elements

    >>> em = InlineElement('em','//')
    >>> mo1 = em.regexp.search('a //word// in a line')
    >>> mo2 = em.regexp.search('a //word in a line\n or two\n')
    >>> mo1.group(0),mo1.group(1)
    ('//word//', 'word')
    >>> mo2.group(0),mo2.group(1)
    ('//word in a line\n or two', 'word in a line\n or two')

    Use a list for the ``token`` argument to have different start
    and end strings. These must be closed.

    >>> foo = InlineElement('foo',['<<','>>'])
    >>> mo = foo.regexp.search('blaa <<here it is >>\n')
    >>> mo.group(1)
    'here it is '

    """

    def __init__(self, tag='', token=''):
        super(InlineElement,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def re_string(self):
        if isinstance(self.token,str):
            content = '(.+?)'
            end = '(' + esc_neg_look + re.escape(self.token) + r'|$)'
            return esc_neg_look + re.escape(self.token) + content + end
        else:
            content = '(.+?)'
            return esc_neg_look + re.escape(self.token[0]) + content + esc_neg_look + re.escape(self.token[1])

    def _process(self, mos, text, wiki_elements, element_store, environ):
        """Returns genshi Fragments (Elements and text)"""
        parts = []
        end = 0
        for mo in mos:
            processed = self._build(mo,element_store, environ)
            store_id = str(id(processed))
            element_store[store_id] = processed
            parts.append(''.join([text[end:mo.start()],'<<<',store_id,'>>>']))
            end = mo.end()
        # call again for trailing text and extend the result list
        if end < len(text):
            parts.append(text[end:])
        new_text = ''.join(parts)
        if not isinstance(wiki_elements[0],(list,tuple)):
            wiki_elements = wiki_elements[1:]
        frags = fragmentize(new_text,wiki_elements,element_store, environ)
        return frags

class SimpleElement(InlineElement):

    r"""For finding generic inline elements like ``strong`` and ``em``.

    >>> em = SimpleElement({'//':'em'})
    >>> mo1 = em.regexp.search('a //word// in a line')
    >>> mo2 = em.regexp.search('a //word in a line\n or two\n')
    >>> mo1.group(0),mo1.group(2)
    ('//word//', 'word')
    >>> mo2.group(0),mo2.group(2)
    ('//word in a line\n or two', 'word in a line\n or two')

    """

    def __init__(self, token_dict={}):
        self.token_dict = token_dict
        self.tokens = token_dict.keys()
        super(SimpleElement,self).__init__('','')
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def re_string(self):
        if isinstance(self.token,basestring):
            tokens = '(' + '|'.join([re.escape(token) for token in self.tokens]) + ')'
            content = '(.+?)'
            end = '(' + esc_neg_look + r'\1|$)'
            return esc_neg_look + tokens + content + end

    def _build(self,mo,element_store, environ):
        return bldr.tag.__getattr__(self.token_dict[mo.group(1)])(fragmentize(mo.group(2),
                                                          self.child_elements,
                                                          element_store, environ))

class CustomElement(InlineElement):

    """Finds markup defined by provided custom regexp."""

    def __init__(self, reg_exp, func):
        super(CustomElement,self).__init__('','')
        if isinstance(reg_exp, basestring):
            self.regexp = re.compile(esc_neg_look + re.escape(reg_exp), re.DOTALL)
        else:
            self.regexp = reg_exp
        self.func = func

    def _build(self,mo,element_store, environ):
        if isinstance(self.func, basestring) and not isinstance(self.func, bldr.Markup):
            value = bldr.tag(bldr.Markup(self.func))
        else:
            value = self.func(mo, environ)
            if isinstance(value, basestring) and not isinstance(value, bldr.Markup):
                raise Exception("Custom markup functions can only return Genshi objects.")

        return value

class LinkElement(InlineElement):

    """Superclass for AnchorLinks and ImageLinks. Parses internal, external,
    and interwiki links.

    """

    def __init__(self,tag, token, delimiter,
                 interwiki_delimiter,base_urls,links_funcs,
                 interwiki_class_funcs,default_space_char,space_chars,
                 base_url,space_char,class_func,path_func,fragment_pattern):
        super(LinkElement,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.DOTALL)
        self.delimiter = delimiter
        self.interwiki_delimiter = interwiki_delimiter
        self.base_urls = base_urls
        self.links_funcs = links_funcs
        self.class_funcs = interwiki_class_funcs
        self.default_space_char = default_space_char
        self.space_chars = space_chars
        self.base_url = base_url
        self.space_char = space_char
        self.class_func = class_func
        self.path_func = path_func
        self.fragment_pattern = fragment_pattern
        self.content_regexp = re.compile(self.content_re_string(),re.DOTALL)
        self.interwikilink_regexp = re.compile(self.interwikilink_re_string())
        self.urllink_regexp = re.compile(self.urllink_re_string(), re.DOTALL)
        self.wikilink_regexp = re.compile(self.wikilink_re_string())

    def content_re_string(self):
        return r'(?P.*?)(' + re.escape(self.delimiter) + '(?P<arg_string>.*?))?$'

    def interwikilink_re_string(self):
        all_wikis = set(self.links_funcs.keys() + self.base_urls.keys())
        wiki_id = '(?P<wiki_id>' + '|'.join(all_wikis) + ')'
        optional_spaces = ' *'
        page_name = r'(?P<page_name>\S+?( \S+?)*)' #allows any number of single spaces
        return '^' + optional_spaces + wiki_id + \
               re.escape(self.interwiki_delimiter) + ' *' + page_name + \
               optional_spaces + '$'

    def urllink_re_string(self):
        protocol = r'^\s*((\w+?:|/)'
        rest_of_url = r'[\S\n]*?)\s*$'
        return protocol + rest_of_url

    def wikilink_re_string(self):
        optional_spaces = ' *'
        if self.fragment_pattern:
            page_name = r'(?P<page_name>\S*?( \S+?)*?)(?P<fragment>' + \
                        self.fragment_pattern + ')?'
        else:
            page_name = r'(?P<page_name>\S+?( \S+?)*?)' #allows any number of single spaces
        return '^' + optional_spaces + page_name + optional_spaces + '$'

    def page_name(self,mo):
        if 'wiki_id' in mo.groupdict():
            space_char = self.space_chars.get(mo.group('wiki_id'),self.default_space_char)
        else:
            space_char = self.space_char
        return mo.group('page_name').replace(' ',space_char)

    def _build(self,mo,element_store, environ):
        content_mo = self.content_regexp.match(mo.group(1))
        body = content_mo.group('body')
        arg_string = content_mo.group('arg_string')
        the_class = None
        if self.interwikilink_regexp.match(body):
            interwikilink_mo = self.interwikilink_regexp.match(body)
            link_type = 'interwiki'
            base_url = self.base_urls.get(interwikilink_mo.group('wiki_id'))
            link_func = self.links_funcs.get(interwikilink_mo.group('wiki_id'))
            class_func = self.class_funcs.get(interwikilink_mo.group('wiki_id'))
            page_name = self.page_name(interwikilink_mo)
            if link_func:
                url = link_func(page_name)
            else:
                url = urllib.quote(page_name.encode('utf-8'))
            if base_url:
                url = urlparse.urljoin(base_url, url)
            if class_func:
                the_class = class_func(page_name)
        elif self.urllink_regexp.match(body):
            urllink_mo = self.urllink_regexp.match(body)
            link_type = 'external'
            if sanitizer.is_safe_uri(urllink_mo.group(1)):
                url = urllink_mo.group(1)
            else:
                url = None
        elif self.wikilink_regexp.match(body):
            wikilink_mo = self.wikilink_regexp.match(body)
            link_type = 'wiki'
            page_name = self.page_name(wikilink_mo)
            if self.path_func and page_name:
                the_path = self.path_func(page_name)
            else:
                the_path = urllib.quote(page_name.encode('utf-8'))
            if wikilink_mo.groupdict().get('fragment'):
                the_path = ''.join([the_path,wikilink_mo.group('fragment')])
            if self.class_func:
                the_class = self.class_func(page_name)
            url = urlparse.urljoin(self.base_url, the_path)
        else:
            url = None

        if not url:
            return mo.group(0)
        else:
            if arg_string is not None:
                args, kw_args = [arg_string.strip()], {}
            else:
                args, kw_args = [], {}
            try:
                return self.emit(element_store, environ,link_type,body,url,the_class, *args, **kw_args)
            except TypeError:
                return mo.group(0)

class AnchorElement(LinkElement):

    """Finds and builds internal, external, and interwiki links.

    >>> link = AnchorElement('a',('[[',']]'),delimiter='|',
    ... interwiki_delimiter=':',
    ... base_urls=dict(somewiki='http://somewiki.org/',
    ...                bigwiki='http://bigwiki.net/'),
    ... links_funcs={},interwiki_class_funcs={},default_space_char='-',
    ... space_chars={'bigwiki':' '},base_url='http://somewiki.org/',
    ... space_char='_',class_func=None,path_func=None,external_links_class=None,
    ... fragment_pattern=r'#![a-z0-9-_]+')

    >>> mo = link.regexp.search("[[http://www.google.com| here]]")
    >>> link._build(mo,{},None).generate().render()
    'here'

    >>> mo = link.regexp.search(" [[somewiki:Home Page|steve]] ")
    >>> link._build(mo,{},None).generate().render()
    'steve'

    >>> mo = link.regexp.search(" [[bigwiki:Home Page]] ")
    >>> link._build(mo,{},None).generate().render()
    'bigwiki:Home Page'

    >>> mo = link.regexp.search(" [[Home Page |Home]]")
    >>> link._build(mo,{},None).generate().render()
    'Home'

    >>> mo = link.regexp.search(" [[#!my-header|Subpage]]")
    >>> link._build(mo,{},None).generate().render()
    'Subpage'

    >>> mo = link.regexp.search(" [[Home Page#!a-header |Home]]")
    >>> link._build(mo,{},None).generate().render()
    'Home'

    """

    def __init__(self, tag, token, external_links_class, *args, **kw_args):
        super(AnchorElement,self).__init__(tag, token, *args, **kw_args)
        self.external_links_class = external_links_class

    def emit(self,element_store, environ,link_type,body,url,the_class, alias=None):
        if alias:
            alias = fragmentize(alias,self.child_elements,element_store, environ)
        else:
            alias = body.strip()
        if link_type == 'external':
            the_class = self.external_links_class
        return bldr.tag.__getattr__(self.tag)(alias,
                                              href=url,
                                              class_=the_class)

class ImageElement(LinkElement):

    """Processes image elements.

    >>> img = ImageElement('img',('{{','}}'), delimiter='|',
    ... interwiki_delimiter=':',
    ... base_urls=dict(somewiki='http://somewiki.org/',
    ...                bigwiki='http://bigwiki.net/'),
    ... links_funcs={},interwiki_class_funcs={},default_space_char='-',
    ... space_chars={'bigwiki':' '},base_url='/images/',
    ... space_char='_',class_func=None,path_func=None,disable_external=False,
    ... fragment_pattern=False)

    >>> mo = img.regexp.search('{{ picture.jpg | An image of a house }}')
    >>> img._build(mo,{},None).generate().render()
    ''

    """

    def __init__(self, tag, token, disable_external, *args, **kw_args):
        super(ImageElement,self).__init__(tag, token, *args, **kw_args)
        self.disable_external = disable_external

    def emit(self,element_store, environ,link_type,body,url,the_class, alt=None):

        if self.disable_external and link_type == 'external':
            return bldr.tag.span('External images are disabled', class_='external_image')

        if alt is None:
            if link_type == 'external':
                alt = urlparse.urlsplit(url).path.split('/')[-1]
            else:
                alt = body.strip()

        return bldr.tag.__getattr__(self.tag)(src=url ,alt=alt, title=alt,
                                              #class_=the_class
                                              )

class Link(InlineElement):

    """DEPRECIATED    """

    def __init__(self,tag, token):
        super(Link,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.DOTALL)
        warnings.warn("""
Use of elements.Link is depreciated.
"""
                  )

    def _build(self,mo,element_store, environ):

        for tag in self.child_elements:
            m = tag.regexp.search(mo.group(1))
            if m:
                link = tag._build(m,element_store, environ)
                if link:
                    break
        else:
            link = None

        if link:
            return bldr.tag(link)
        else:
            return mo.group(0)

class Macro(WikiElement):
    r"""Finds and processes inline macro elements."""

    def __init__(self, tag, token, func, macros, arg_parser):
        super(Macro,self).__init__(tag,token , [])
        self.func = func
        self.macros = macros
        self.arg_parser = arg_parser
        self.regexp = re.compile(self.re_string())

    def _process(self, mos, text, wiki_elements,element_store, environ):
        """Returns genshi Fragments (Elements and text)"""
        assert len(mos) == 1
        mo = mos[0]
        processed = self._build(mo,element_store, environ)
        if isinstance(processed, list):
            tail = processed[1]
            processed = processed[0]
        else:
            tail = ''
        if isinstance(processed, basestring) and not isinstance(processed,Markup):
            text = ''.join([text[:mo.start()],processed,tail,
                        text[mo.end():]])
        else:
            store_id = str(id(processed))
            element_store[store_id] = processed
            text = ''.join([text[:mo.start()],'<<<',store_id,'>>>',tail,
                        text[mo.end():]])
        frags = fragmentize(text,wiki_elements,element_store, environ)
        return frags

    def re_string(self):
        content = '(.*?)'
        return esc_neg_look + re.escape(self.token[0]) + r'(' + MACRO_NAME + \
               content + ')' + esc_neg_look + re.escape(self.token[1])

    def _macro_func(self,macro_name,arg_string,body,isblock,environ):

        func = self.macros[macro_name]
        arg_parser = func.func_dict.get('arg_parser') or self.arg_parser
        if arg_parser:
            pos, kw = arg_parser(arg_string)
        else:
            pos, kw = [], {}

        def parsed_body(context='auto'):
            if context == 'auto':
                if isblock:
                    child_elements = self.dialect.block_elements
                else:
                    child_elements = self.dialect.inline_elements
            elif context == 'inline':
                child_elements = self.dialect.inline_elements
            elif context == 'block':
                child_elements = self.dialect.block_elements
            else:
                child_elements = context
            return bldr.tag(fragmentize(body, child_elements, {}, environ))

        macro = AttrDict(name=macro_name,arg_string=arg_string,
                         body=body, isblock=isblock, parsed_body=parsed_body)

        try:
            value = func(macro,environ,*pos,**kw)
        except TypeError , detail:
            tag = isblock and 'pre' or 'code'
            e = str(detail)
            msg = re.sub(r"^\w*\(\) ", '', e)
            mo = re.match(r'(.+)(\d+)(.+)(\d+) given\)$',msg)
            if mo: #re.search(r'given\)$',msg):
                #mo = re.match(r'(.+)(\d+)(.+)(\d+)(.+)$',msg)
                msg = mo.group(1) + str(int(mo.group(2))-2) \
                      + re.sub('arguments?','argument(s)',mo.group(3)) \
                      + str(int(mo.group(4))-2) + ' given)'
            value = bldr.tag.__getattr__(tag)("Macro error: '%s' %s"% (macro_name, msg),class_='macro_error')
        except MacroError, detail:
            tag = isblock and 'pre' or 'code'
            value = bldr.tag.__getattr__(tag)("Error in macro '%s': %s"% (macro_name,str(detail.value)),class_='macro_error')
        except:
            value = bldr.tag.pre('Unexpected error during macro execution!\n'
                                 + traceback.format_exc(20) ,
                                 class_='macro_error')

        return value

    trailing_slash = re.compile(r'(?<=[ "\'\]])/$')
    def _build(self,mo,element_store, environ):
        arg_string = re.sub(self.trailing_slash,'',mo.group(4))

        if mo.group('name') in self.macros:
            value = self._macro_func(mo.group('name'),arg_string,None,False,environ)
        elif self.func:
            value = self.func(mo.group('name'),arg_string,None,False,environ)
        else:
            value = None

        if value is None:
            return bldr.tag.code(self.token[0],bldr.tag.span(mo.group('name'),class_="macro_name"),
                           bldr.tag.span(arg_string,class_="macro_arg_string"),
                           self.token[1],class_="unknown_macro")
        elif isinstance(value, (basestring,bldr.Fragment,bldr.Element, Stream)):
            return value
        else:
            raise Exception("macros can only return strings and genshi objects")

class BodiedMacro(Macro):
    """Finds and processes macros with bodies.

    Does not span across top level block markup
    (see BodiedBlockMacro's for that)."""

    def __init__(self, tag, token, func, macros, arg_parser):
        super(BodiedMacro,self).__init__(tag,token , func, macros, arg_parser)
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def re_string(self):
        content = r'(?P<arg_string>[ \S]*?)'
        body = '(?P.+)'
        return esc_neg_look + re.escape(self.token[0]) + MACRO_NAME + \
               content + '(?<!/)' + re.escape(self.token[1]) + \
               body + esc_neg_look + re.escape(self.token[0]) + \
               r'/(?P=name)' + '(?<!/)' + re.escape(self.token[1])

    def _build(self,mo,element_store, environ):
        start = ''.join([esc_neg_look, re.escape(self.token[0]), re.escape(mo.group('name')),
                         r'(?P<arg_string>[ \S]*?)', re.escape(self.token[1])])
        end = ''.join([esc_neg_look, re.escape(self.token[0]), '/', re.escape(mo.group('name')),
                       re.escape(self.token[1])])
        count = 0
        for mo2 in re.finditer(start + '|' + end, mo.group('body')):
            if re.match(end,mo2.group(0)):
                count = count + 1
            else:
                count = count - 1
            if count > 0:
                body = mo.group('body')[:mo2.start()]
                tail = ''.join([mo.group('body')[mo2.end():], self.token[0],
                                '/', mo.group('name'), self.token[1]])
                break
        else:
            body = mo.group('body')
            tail = ''

        if mo.group('name') in self.macros:
            value = self._macro_func(mo.group('name'),mo.group('arg_string'),body,False,environ)
        elif self.func:
            value = self.func(mo.group('name'),mo.group('arg_string'),body,False,environ)
        else:
            value = None

        if value is None:
            content_out = [self.token[0],bldr.tag.span(mo.group('name'),class_="macro_name"),
                           bldr.tag.span(mo.group('arg_string'),class_="macro_arg_string"),
                           self.token[1],bldr.tag.span(mo.group('body'),class_="macro_body"),
                           self.token[0] + '/' + mo.group('name') + self.token[1]]
            output = bldr.tag.code(content_out,class_="unknown_macro", style="white-space:pre-wrap")
        elif isinstance(value, (basestring,bldr.Fragment, Stream)):
            output = value
        else:
            raise Exception("macros can only return strings and genshi objects")

        return [output, tail]

class BodiedBlockMacro(Macro):
    """Finds and processes block macros with bodies.

    The opening and closing tokens must be are each on a line alone without
    leading spaces. These macros can enclose other block level markup
    including pre blocks and other BodiedBlockMacro's."""
    def __init__(self, tag, token, func, macros, arg_parser):
        super(BodiedBlockMacro,self).__init__(tag,token , func, macros, arg_parser)
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        arg_string = r'(?P<arg_string>(?![^\n]*>>[^\n]*>>)[ \S]*?)'
        start = '^' + re.escape(self.token[0])
        body = r'(?P.*\n)'
        end = re.escape(self.token[0]) + \
               r'/(?P=name)' + '(?<!/)' + re.escape(self.token[1]) + r'\s*?$'

        return start + '(' + MACRO_NAME + arg_string + ')' + '(?<!/)' + \
               re.escape(self.token[1]) + r'\s*?\n' + body + end

    def _process(self, mos, text, wiki_elements,element_store, environ):
        """Returns genshi Fragments (Elements and text)

        This is mainly for block level markup. See InlineElement
        for the other method.
        """
        assert len(mos) == 1
        mo = mos[0]
        processed = self._build(mo,element_store, environ)
        if isinstance(processed, list):
            tail = processed[1]
            processed = processed[0]
        else:
            tail = ''
        if isinstance(processed, basestring) and not isinstance(processed,Markup):
            text = ''.join([text[:mo.start()],processed,tail,
                        text[mo.end():]])
            frags = fragmentize(text,wiki_elements,element_store, environ)
        else:

            frags = []
            # call again for leading text and extend the result list
            if mo.start():
                frags.extend(fragmentize(text[:mo.start()],wiki_elements[1:],
                                         element_store, environ))
            # append the found wiki element to the result list
            frags.append(processed)
            # make the source output easier to read
            if self.append_newline:
                frags.append('\n')
            # call again for trailing text and extend the result list
            if tail or mo.end() < len(text):
                frags.extend(fragmentize(tail + text[mo.end():],wiki_elements,
                                         element_store, environ))
        return frags

    def _build(self,mo,element_store, environ):
        start = ''.join(['^', re.escape(self.token[0]), re.escape(mo.group('name')),
                         r'(?P<arg_string>(?![^\n]*>>[^\n]*>>)[ \S]*?)', re.escape(self.token[1]),r'\s*?\n'])
        end = ''.join(['^', re.escape(self.token[0]), '/', re.escape(mo.group('name')),
                       re.escape(self.token[1]),r'\s*?$'])
        count = 0
        for mo2 in re.finditer(start + '|' + end, mo.group('body'),re.MULTILINE):
            if re.match(end,mo2.group(0)):
                count = count + 1
            else:
                count = count - 1
            if count > 0:
                body = mo.group('body')[:mo2.start()]
                tail = ''.join([mo.group('body')[mo2.end():], self.token[0],
                                '/', mo.group('name'), self.token[1],'\n'])
                break
        else:
            body = mo.group('body')
            tail = ''

        if mo.group('name') in self.macros:
            value = self._macro_func(mo.group('name'),mo.group('arg_string'),body,True,environ)
        elif self.func:
            value = self.func(mo.group('name'),mo.group('arg_string'),body,True,environ)
        else:
            value = None

        if value is None:
            output = bldr.tag.pre(self.token[0],bldr.tag.span(mo.group('name'),class_="macro_name"),
                           bldr.tag.span(mo.group('arg_string'),class_="macro_arg_string"),
                           self.token[1],'\n',bldr.tag.span(mo.group('body'),class_="macro_body"),
                           self.token[0] + '/' + mo.group('name') + self.token[1],
                           class_="unknown_macro")
        elif (isinstance(value, (Stream, basestring)) or
             (isinstance(value,bldr.Element) and value.tag in BLOCK_TAGS)):
            output = value
        # Add a p tag if the value is a Fragment or Element that needs one
        elif isinstance(value, bldr.Fragment):
            output = bldr.tag.p(value)
        else:
            raise Exception("macros can only return strings and genshi objects")

        return [output, tail]

class RawLink(InlineElement):

    """Used to find raw urls in wiki text and build xml from them.

    >>> raw_link = RawLink(tag='a')
    >>> mo = raw_link.regexp.search(" a http://www.google.com url ")
    >>> raw_link.href(mo)
    'http://www.google.com'
    >>> raw_link._build(mo,{},None).generate().render()
    'http://www.google.com'

    """
    linking_protocols = ['http','https']

    def __init__(self, tag):
        super(RawLink,self).__init__(tag=tag, token=None)
        self.regexp = re.compile(self.re_string())

    def re_string(self):
        escape = '(' + re.escape(escape_char) + ')?'
        protocol = '((https?|ftp)://'
        rest_of_url = r'\S+?)'
        #allow one punctuation character or '**' or '//'. Don't include a placeholder.
        look_ahead = r'(?=([>)}\]]?[,.?!:;"\']?(([^a-zA-Z0-9])\6)?(\s|$))|<<<)'
        return escape + protocol + rest_of_url + look_ahead

    def _build(self,mo,element_store, environ):
        if (not mo.group(1)) and (mo.group(3) in self.linking_protocols):
            return bldr.tag.__getattr__(self.tag)(self.alias(mo,element_store),
                                              href=self.href(mo))
        else:
            return self.href(mo)

    def href(self,mo):
        """Returns the string for the href attribute of the Element."""
        if sanitizer.is_safe_uri(mo.group(2)):
            return mo.group(2)
        else:
            return "unsafe_uri_detected"

    def alias(self,mo,element_store):
        """Returns the string for the content of the Element."""
        return self.href(mo)

class URLLink(WikiElement):

    """DEPRECIATED    """

    def __init__(self, tag,delimiter):
        super(URLLink,self).__init__(tag, '')
        self.delimiter = delimiter
        self.regexp = re.compile(self.re_string(),re.DOTALL)
        warnings.warn("""
Use of elements.URLLink is depreciated.
"""
                  )

    def re_string(self):
        protocol = r'^\s*((\w+?:|/)'
        rest_of_url = r'[\S\n]*?)\s*'
        alias = r'(' + re.escape(self.delimiter) + r' *(.*?))? *$'
        return protocol + rest_of_url + alias

    def _build(self,mo,element_store, environ):
        if not self.href(mo):
            return None
        return bldr.tag.__getattr__(self.tag)(self.alias(mo,element_store, environ),
                                              href=self.href(mo))

    def href(self,mo):
        """Returns the string for the href attribute of the Element."""
        if sanitizer.is_safe_uri(mo.group(1)):
            return mo.group(1)
        else:
            return None

    def alias(self,mo,element_store, environ):
        """Returns the string for the content of the Element."""
        if not mo.group(4):
            return self.href(mo)
        else:
            return fragmentize(mo.group(4),self.child_elements,element_store, environ)

class InterWikiLink(WikiElement):

    """DEPRECIATED    """

    def __init__(self, tag, delimiter1,
                 delimiter2,base_urls,links_funcs,default_space_char,space_chars):
        super(InterWikiLink,self).__init__(tag, '')
        self.delimiter1 = delimiter1
        self.delimiter2 = delimiter2
        #self.regexp = re.compile(self.re_string())
        self.base_urls = base_urls
        self.links_funcs = links_funcs
        self.default_space_char = default_space_char
        self.space_chars = space_chars
        self.regexp = re.compile(self.re_string())
        warnings.warn("""
Use of elements.InterWikiLink is depreciated.
"""
                  )

    def re_string(self):
        #all_wikis = set(self.links_funcs.keys() + self.base_urls.keys())
        #wiki_id = '(' + '|'.join(all_wikis) + ')'

        wiki_id = r'(\w+)'
        optional_spaces = ' *'
        page_name = r'(\S+?( \S+?)*)' #allows any number of single spaces
        alias = r'(' + re.escape(self.delimiter2) + r' *(.*?))? *$'
        return '^' + optional_spaces + wiki_id + optional_spaces + \
               re.escape(self.delimiter1) + optional_spaces + page_name + \
               optional_spaces + alias

    def page_name(self,mo):
        space_char = self.space_chars.get(mo.group(1),self.default_space_char)
        return mo.group(2).replace(' ',space_char)

    def href(self,mo):
        linktype = mo.group(1)
        base_url = self.base_urls.get(linktype)
        link_func = self.links_funcs.get(linktype)
        if not (link_func or base_url):
            return None
        else:
            href = self.page_name(mo)
            if link_func:
                href = link_func(href)
            else:
                href = urllib.quote(href.encode('utf-8'))
            if base_url:
                href = urlparse.urljoin(base_url, href)
            return href

    def _build(self,mo,element_store, environ):
        if not self.href(mo):
            return '[[' + mo.group(0) + ']]'
        return bldr.tag.__getattr__(self.tag)(self.alias(mo,element_store, environ),
                                              href=self.href(mo))
    def alias(self,mo,element_store, environ):
        """Returns the string for the content of the Element."""
        if not mo.group(5):
            return ''.join([mo.group(1),self.delimiter1,mo.group(2)])
        else:
            return fragmentize(mo.group(5),self.child_elements,element_store, environ)

class WikiLink(WikiElement):

    """DEPRECIATED    """

    def __init__(self, tag, delimiter,
                 base_url,space_char,class_func,path_func):
        super(WikiLink,self).__init__(tag, '')
        self.delimiter = delimiter
        self.base_url = base_url
        self.space_char = space_char
        self.class_func = class_func
        self.path_func = path_func
        self.regexp = re.compile(self.re_string())
        warnings.warn("""
Use of elements.WikiLink is depreciated.
"""
                  )

    def re_string(self):
        optional_spaces = ' *'
        page_name = r'(\S+?( \S+?)*?)' #allows any number of single spaces
        alias = r'(' + re.escape(self.delimiter) + r' *(.*?))? *$'
        return '^' + optional_spaces + page_name + optional_spaces + \
               alias

    def page_name(self,mo):
        return mo.group(1).replace(' ',self.space_char)

    def href(self,mo):
        if self.path_func:
            the_path = self.path_func(self.page_name(mo))
        else:
            the_path = urllib.quote(self.page_name(mo).encode('utf-8'))
        return urlparse.urljoin(self.base_url, the_path)

    def _build(self,mo,element_store, environ):
        if self.class_func:
            the_class = self.class_func(self.page_name(mo))
        else:
            the_class = None
        return bldr.tag.__getattr__(self.tag)(self.alias(mo,element_store, environ),
                                              href=self.href(mo),
                                              class_=the_class)

    def alias(self,mo,element_store, environ):
        """Returns the string for the content of the Element."""
        if not mo.group(3):
            return mo.group(1)
        else:
            return fragmentize(mo.group(4),self.child_elements,element_store, environ)

class List(BlockElement):

    """Finds list (ordered, unordered, and definition) wiki elements.

    group(1) of the match object includes all lines from the list
    including newline characters.

    """

    def __init__(self, tag, token,stop_tokens=None):
        self.stop_tokens = stop_tokens
        super(List,self).__init__(tag, token)
        #self.stop_tokens = stop_tokens
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        """This re_string is for finding generic block elements like
        lists (ordered, unordered, and definition) that start with a
        single token.
        """
        leading_whitespace = r'^([ \t]*'
        only_one_token = re.escape(self.token)+ '(?!' + re.escape(self.token) + ')'
        rest_of_list = r'.*?(?:\n|\Z))'
        only_one_stop_token = '([' + re.escape(self.stop_tokens) + r'])(?!\3)'
        look_ahead = '(?=([ \t]*' + only_one_stop_token + '|$))'
        return leading_whitespace + only_one_token + rest_of_list + \
               look_ahead

class ListItem(BlockElement):
    r"""Matches the current list item.

    Everything up to the next same-level list item is matched.

    >>> list_item = ListItem('li','#*')
    >>> mo = list_item.regexp.search("*one\n**one.1\n**one.2\n*two\n")
    >>> mo.group(3)
    'one\n**one.1\n**one.2\n'
    >>> mo.group(0)
    '*one\n**one.1\n**one.2\n'

    """

    append_newline = False

    def __init__(self, tag, list_tokens):
        """Constructor for list items.

        :parameters"
          list_tokens
            A string that includes the tokens used for lists
        """
        self.list_tokens = list_tokens
        super(ListItem,self).__init__(tag, None)
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def re_string(self):
        whitespace = r'[ \t]*'
        item_start = '(([' + self.list_tokens + r'])\2*)'
        rest_of_item = r'(.*?(?:\n|\Z))'
        start_of_same_level_item = r'\1(?!\2)'
        look_ahead = r'(?=(' + whitespace + start_of_same_level_item + '|$))'
        return whitespace + item_start + whitespace + \
               rest_of_item + look_ahead

    def _build(self,mo,element_store, environ):
        return bldr.tag.__getattr__(self.tag)(fragmentize(mo.group(3),
                                                          self.child_elements,
                                                          element_store, environ))

class NestedList(WikiElement):

    r"""Finds a list in the current list item.

    >>> nested_ul = NestedList('ul','*')
    >>> mo = nested_ul.regexp.search('one\n**one.1\n**one.2\n')
    >>> mo.group(1)
    '**one.1\n**one.2\n'
    >>> mo.group(0) == mo.group(1)
    True

    """

    def __init__(self, tag, token):
        super(NestedList,self).__init__(tag, token)
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        look_behind = r'(?<=\n)' # have to avoid finding a list on the first line
        whitespace = r'(\s*'
        rest_of_list = '.*$)'
        return look_behind + '^' + whitespace + re.escape(self.token) + \
               rest_of_list

class DefinitionTerm(BlockElement):

    r"""Processes definition terms.

    >>> term = DefinitionTerm('dt',';',stop_token=':')
    >>> mo1,mo2 = term.regexp.finditer(";term1\n:def1\n;term2:def2\n")
    >>> mo1.group(1), mo2.group(1)
    ('term1', 'term2')
    >>> mo1.group(0), mo2.group(0)
    (';term1\n', ';term2')

    group(1) of the match object is the term line or up to the first ':'

    """

    def __init__(self, tag, token,stop_token):
        super(DefinitionTerm,self).__init__(tag, token)
        self.stop_token = stop_token
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        look_ahead = r'(\n|(?=(' + esc_neg_look + re.escape(self.stop_token) + r'|$)))'
        return r'^[ \t]*' + re.escape(self.token) + r'[ \t]*(.*?' + \
               re.escape(self.stop_token) +  '?)\s*' + look_ahead

class DefinitionDef(BlockElement):

    r"""Processes definitions.

    >>> definition = DefinitionDef('dd',':')
    >>> mo1,mo2 = definition.regexp.finditer(":def1a\ndef1b\n:def2\n")
    >>> mo1.group(1), mo2.group(1)
    ('def1a\ndef1b', 'def2')
    >>> mo1.group(0), mo2.group(0)
    (':def1a\ndef1b\n', ':def2\n')

    group(1) of the match object includes all lines from the defintion
    up to the next definition.

    """

    def __init__(self, tag, token):
        super(DefinitionDef,self).__init__(tag, token)
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        look_ahead = r'(?=(^[ \t]*' + re.escape(self.token) + r')|\Z)'
        return r'^[ \t]*' + re.escape(self.token) + r'?[ \t]*(.+?)\s*' + look_ahead

class Paragraph(BlockElement):
    """"This should be the last outer level wiki element to be searched.

    Anything that is left over will be placed in a paragraphs unless it looks
    like block content according to xhtml1 strict. Block content is defined
    here as valid children of the  element (see BLOCK_TAGS). Only genshi
    Element objects will be evaluated (see BLOCK_TAGS). Fragments and stings
    are treated as inline while Streams are block content.

    """

    def __init__(self, tag):
        super(Paragraph,self).__init__(tag,None)
        self.regexp = re.compile(self.re_string(),re.DOTALL)#+re.MULTILINE)

    def re_string(self):
        return r'^(.*?)\n?$'

    def _build(self,mo,element_store, environ):
        content = fragmentize(mo.group(1), self.child_elements, element_store, environ)
        # Check each list item and record those that are block only
        block_only_frags = []
        for i,element in enumerate(content):
            if ((isinstance(element, bldr.Element) and
                element.tag in BLOCK_ONLY_TAGS) or
                isinstance(element,(Stream,Markup))):
                block_only_frags.append(i)
        # Build a new result list if needed
        if block_only_frags:
            new_content = []
            last_i = -1
            for i in block_only_frags:
                if content[last_i+1:i]:
                    if not (len(content[last_i+1:i])==1 and
                                                content[last_i+1] == '\n'):
                        new_content.append(bldr.tag.__getattr__(self.tag)(content[last_i+1:i]))
                    else:
                        new_content.append('\n')
                new_content.append(content[i])
                last_i = i
            if content[last_i+1:]:
                new_content.append(bldr.tag.__getattr__(self.tag)(content[last_i+1:]))
            return bldr.tag(new_content)
        else:
            return bldr.tag.__getattr__(self.tag)(content)

class Heading(BlockElement):

    r"""Processes heading wiki elements. Optionally adds id attributes.

    >>> h1 = Heading(['h1','h2'],'=',id_prefix=False)
    >>> mo = h1.regexp.search('before\n = An important thing = \n after')
    >>> mo.group(2)
    'An important thing'
    >>> mo.group(0)
    ' = An important thing = \n'

    >>> h1 = Heading(['h1','h2'],'=',id_prefix='!')
    >>> mo = h1.regexp.search(u'before\n = An important thing = \n after')
    >>> mo.group(2)
    u'An important thing'
    >>> h1.make_id('!',mo.group(2),[])
    '!an-important-thing'
    >>> h1.make_id('!',mo.group(2),['!an-important-thing'])
    '!an-important-thing_1'
    >>> h1.make_id('!',mo.group(2),{})
    '!an-important-thing'
    >>> h1.make_id('!',mo.group(2) + '!@#$%&*()_[]+=/?|-- TO    SAY',{})
    '!an-important-thing-to-say'

    """

    def __init__(self, tag, token, id_prefix):
        super(Heading,self).__init__('',token)
        self.id_prefix = id_prefix
        self.tags = tag
        self.regexp = re.compile(self.re_string(),re.MULTILINE)

    def make_id(self, prefix, heading_text,used_ids):
        slug = unicodedata.normalize('NFKD', heading_text).encode('ascii', 'ignore').lower()
        slug = re.sub('-+', '-', re.sub('[^a-z0-9-]+', '-', slug)).strip('-')
        slug = ''.join([prefix,slug]) or '!'
        # Restrict length without breaking words.
        while len(slug) > 40:
            if slug.find('-') == -1:
                slug = slug[:40]
            else:
                slug = slug.rsplit('-', 1)[0]
        i = 1
        original_slug = slug
        while True:
            if slug not in used_ids:
                break
            slug = '%s_%d' % (original_slug, i)
            i += 1

        return slug

    def re_string(self):
        whitespace = r'[ \t]*'
        tokens = '(' + re.escape(self.token) + '{1,' + str(len(self.tags)) +'})'
        content = '(.*?)'
        trailing_markup = '(' + re.escape(self.token) + r'+[ \t]*)?(\n|\Z)'
        return '^' + whitespace + tokens + \
               whitespace + content + whitespace + trailing_markup

    def _build(self,mo,element_store, environ):
        heading_tag = self.tags[len(mo.group(1))-1]
        heading_body = fragmentize(mo.group(2),
                                   self.child_elements,
                                   element_store, environ)
        if self.id_prefix is False:
            id_=None
        else:
            heading_text = bldr.tag(heading_body).generate().render(method='text',encoding=None)
            used_ids = environ.setdefault('ids', [])
            id_ = self.make_id(self.id_prefix,heading_text,used_ids)
            used_ids.append(id_)
            #toc = environ.setdefault('toc', [])
            #toc.append((heading_tag, bldr.tag(heading_body), id_))
        return bldr.tag.__getattr__(heading_tag)(heading_body,
                                                 id_=id_)

class Table(BlockElement):

    r"""Find tables.

    >>> table = Table('table','|')
    >>> mo = table.regexp.search("before\n | one | two |\n|one|two \n hi")
    >>> mo.group(1)
    ' | one | two |\n|one|two \n'
    >>> mo.group(0) == mo.group(1)
    True

    """

    def __init__(self, tag, token):
        super(Table,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.MULTILINE)

    def re_string(self):
        whitespace = r'[ \t]*'
        rest_of_line = r'.*?(\n|\Z)'
        return '^((' + whitespace + re.escape(self.token) + \
               rest_of_line + ')+)'

class TableRow(BlockElement):

    r"""Finds rows in a table.

    >>> row = TableRow('tr','|')
    >>> mo = row.regexp.search(' | one | two |\n|one|two \n')
    >>> mo.group(1)
    '| one | two '
    >>> mo.group(0)
    ' | one | two |\n'

    """

    def __init__(self, tag, token):
        super(TableRow,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.MULTILINE)

    def re_string(self):
        whitespace = r'[ \t]*'
        content = '(' + re.escape(self.token) + '.*?)'
        trailing_token = re.escape(self.token) + '?'
        return '^' + whitespace + content + trailing_token + \
               whitespace + r'(\n|\Z)'

class TableCell(WikiElement):

    r"""Finds cells in a table row.

    >>> cell = TableCell('td','|')
    >>> mo = cell.regexp.search('| one | two ')
    >>> mo.group(1)
    'one'
    >>> mo.group(0)
    '| one '

    """

    def __init__(self, tag, token):
        super(TableCell,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string())

    def re_string(self):
        whitespace = r'[ \t]*'
        content = '(.*?)'
        look_ahead = '((?=' + esc_neg_look + re.escape(self.token[0]) + ')|$)'
        return esc_neg_look + re.escape(self.token) + whitespace + \
               content + whitespace + look_ahead

class Image(InlineElement):

    """DEPRECIATED    """

    def __init__(self, tag, token, delimiter):
        super(Image,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string())
        self.delimiter = delimiter
        self.src_regexp = re.compile(r'^\s*(\S+)\s*$')
        warnings.warn("""
Use of elements.Image is depreciated.
"""
                  )

    def _build(self,mo,element_store, environ):
        body = mo.group(1).split(self.delimiter,1)
        src_mo = self.src_regexp.search(body[0])
        if not src_mo:
            return bldr.tag.span('Bad Image src')
        if sanitizer.is_safe_uri(src_mo.group(1)):
            link = src_mo.group(1)
        else:
            link = "unsafe_uri_detected"
        if len(body) == 1:
            alias = link
        else:
            alias = body[1].strip()
        return bldr.tag.__getattr__(self.tag)(src=link ,alt=alias, title=alias)

class NoWikiElement(InlineElement):

    """Inline no-wiki.

    When two or more end tokens are found together, only last marks
    the end of the element.

    """

    def __init__(self, tag, token):
        super(NoWikiElement,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def _build(self,mo,element_store, environ):
        if self.tag:
            return bldr.tag.__getattr__(self.tag)(
                   fragmentize(mo.group(1), self.child_elements,
                               element_store,environ,
                               remove_escapes=False))
        else:
            return bldr.tag(fragmentize(mo.group(1),self.child_elements,
                                        element_store, environ,
                                        remove_escapes=False))

    def re_string(self):
        if isinstance(self.token,str):
            content = '(.+?' + re.escape(self.token[-1]) + '*)'
            return esc_neg_look + re.escape(self.token) + \
                   content + re.escape(self.token)
        else:
            content = '(.+?' + re.escape(self.token[1][-1]) + '*)'
            return esc_neg_look + re.escape(self.token[0]) + \
                   content + re.escape(self.token[1])

class PreBlock(BlockElement):
    """A preformatted block.

    If a closing token is found on a line with a space as the first
    character, the space will be removed from the output.

    """

    def __init__(self, tag, token ):
        super(PreBlock,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)
        self.regexp2 = re.compile(self.re_string2(),re.MULTILINE)

    def re_string(self):
        if isinstance(self.token,str):
            return '^' + re.escape(self.token) + r'\s*?\n(.*?\n)' + \
                   re.escape(self.token) + r'\s*?\n'
        else:
            start = '^' + re.escape(self.token[0]) + r'\s*?\n'
            content = r'(.+?\n)'
            end = re.escape(self.token[1]) + r'\s*?$'
            return start + content + end

    def re_string2(self):
        """Finds a closing token with a space at the start of the line."""
        if isinstance(self.token,str):
            return r'^ (\s*?' + re.escape(self.token) + r'\s*?\n)'
        else:
            return r'^ (\s*?' + re.escape(self.token[1]) + r'\s*?\n)'

    def _build(self,mo,element_store, environ):
        match = self.regexp2.sub(r'\1',mo.group(1))

        return bldr.tag.__getattr__(self.tag)(
            fragmentize(match,self.child_elements,
                        element_store, environ,remove_escapes=False))

class IndentedBlock(BlockElement):
    """An  indented block.

    """

    def __init__(self, tag, token, class_ , style ):
        super(IndentedBlock,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string(),re.MULTILINE)
        self.regexp2 = re.compile(self.re_string2(),re.MULTILINE)
        self.class_ = class_
        self.style = style

    def re_string(self):
        return r'^((' + re.escape(self.token) \
               + r'.*?(\n|\Z))+)'

    def re_string2(self):
        """Finds a token at the start of the line."""
        return r'^' + re.escape(self.token)

    def _build(self,mo,element_store, environ):
        match = self.regexp2.sub(r'',mo.group(1)) # removes tokens during processing
        return bldr.tag.__getattr__(self.tag)(
            fragmentize(match,self.child_elements,
                        element_store, environ),class_=self.class_,style=self.style)

class LoneElement(BlockElement):
    """Element on a line by itself with no content (e.g.,
)"""

    def __init__(self, tag, token):
        super(LoneElement,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string(),re.DOTALL+re.MULTILINE)

    def re_string(self):
        return r'^(\s*?' + re.escape(self.token) + r'\s*?(\n|\Z))'

    def _build(self,mo,element_store, environ):
        return bldr.tag.__getattr__(self.tag)()

class HrElement(LoneElement):
    def __init__(self):
        super(LoneElement, self).__init__('hr', '----')
        self.regexp = re.compile(r'^(\s*?-{4,}\s*?(\n|\Z))', re.DOTALL + re.MULTILINE)

class BlankLine(WikiElement):

    """Blank lines divide elements but don't add any output."""

    def __init__(self):
        super(BlankLine,self).__init__(tag=None,token='' , child_elements=[])
        self.regexp = re.compile(self.re_string(),re.MULTILINE)

    def re_string(self):
        return r'^\s*(\Z|\n)' # r'^(\s*\n)+'

    def _build(self,mo,element_store, environ):
        return None

    def _process(self, mos, text, wiki_elements,element_store, environ):
        """Returns genshi Fragments (Elements and text)

        Custom _process method here just to avoid unnecessary calling of
        _build.
        """

        frags = []
        end = 0
        for mo in mos:
            if end != mo.start():
            # call again for leading text and extend the result list
                frags.extend(fragmentize(text[end:mo.start()],wiki_elements[1:],
                                         element_store, environ))
            end = mo.end()
        # call again for trailing text and extend the result list
        if end < len(text):
            if not isinstance(wiki_elements[0],(list,tuple)):
                wiki_elements = wiki_elements[1:]
            frags.extend(fragmentize(text[end:],wiki_elements,
                 element_store, environ))

        return frags

class LineBreak(InlineElement):
    """An inline line break."""

    def __init__(self,tag, token, blog_style=False):
        self.blog_style = blog_style
        super(LineBreak,self).__init__(tag,token )
        self.regexp = re.compile(self.re_string(),re.DOTALL)

    def re_string(self):
        if self.blog_style:
            return '(' + esc_neg_look + re.escape(self.token) + r'\n?|\n(?!$))'
        else:
            return esc_neg_look + re.escape(self.token)

    def _build(self,mo,element_store, environ):
        return bldr.tag.__getattr__(self.tag)()

#############################################################################
# The WikeElement classes below are used for parsing macro argument strings #
#############################################################################

class ArgString(WikiElement):
    """Base class for elements used on argument strings"""
    def __init__(self, tag='', token=''):
        super(ArgString,self).__init__(tag,token)
        self.regexp = re.compile(self.re_string(),re.DOTALL)

class KeywordArg(ArgString):
   """Finds keyword arguments"""

   def re_string(self):
      return r'(?P<key>\w[\w0-9]*) *'+re.escape(self.token) + \
               r' *(?P.*?) *(?=\w[\w0-9]* *' + re.escape(self.token) +'|$)'

   def _build(self,mo,element_store, environ):
      if mo.group('body') == '':
         value = ''
      else:
         value = fragmentize(mo.group('body'),self.child_elements,
                             element_store, environ)
         if len(value) == 1:
             value = value[0]
         else:
             value = ImplicitList(value)
      name = mo.group('key')
      return (name, value)

class QuotedArg(InlineElement):
   """Finds quoted arguments"""

   def re_string(self):
      return esc_neg_look + r'(?P<quote>['+ re.escape(self.token) \
             +'])(?P.*?)' + esc_neg_look + '(?P=quote)'

   def _build(self,mo,element_store, environ):
      if mo.group('body') == '':
          value = ''
      else:
          frags = fragmentize(mo.group('body'),self.child_elements,element_store, environ)
          assert len(frags) == 1
          value = frags[0]
      return value

class ListArg(ArgString):
   """Finds lists in argument strings

   This is used for positional arguments only.

   """

   def re_string(self):
      return esc_neg_look + re.escape(self.token[0]) + r'(?P.*?)' + esc_neg_look + re.escape(self.token[1])

   def _build(self,mo,element_store, environ):
      if mo.group('body') == '':
         value = []
      else:
         value = fragmentize(mo.group('body'),self.child_elements,element_store, environ)
      return value

class ExplicitListArg(ListArg):
    """Only finds lists where the string to be searched is fully enclosed

    This is used for keyword values in argument strings.

    """

    def re_string(self):
       return '^' + esc_neg_look + re.escape(self.token[0]) +r'(?P.*?)' \
             + esc_neg_look+ re.escape(self.token[1]) + '$'

class WhiteSpace(ArgString):
   """Breaks up elements but doesn't add any output"""

   def re_string(self):
      return r'[ \n]+'

   def _build(self,mo,element_store, environ):
      return None

def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()


text:
namespace _01.RefactoringClassChef
{
    using System.Collections.Generic;

    public class Bowl
    {
        public Bowl()
        {
            this.Ingredients = new List<Vegetable>();
        }

        public List<Vegetable> Ingredients { get; set; }

        public void Add(Vegetable vegetable)
        {
            this.Ingredients.Add(vegetable);
        }

        public void Cook()
        {
            foreach (Vegetable ingredient in this.Ingredients)
            {
                ingredient.Cook();
            }
        }

        public override string ToString()
        {
            return string.Join(", ", this.Ingredients);
        }
    }
}


text:
using MjModelLibrary.Result;

namespace MjModelLibrary
{
    class TehaiAnalizer
    {

        public bool CanChi(Tehai tehai, int dapaiActor, int playerId, string pai)
        {
            if ((dapaiActor != playerId) && ((dapaiActor + 1) % 4 == playerId))
            {
                // can chi
                return false;
            }
            // cann't chi
            return false;
        }

        public bool CanPon(Tehai tehai, int dapaiActor, int playerId, string pai)
        {
            if ((dapaiActor != playerId))
            {
                // can pon
                return false;
            }
            return false;
        }

        public bool CanHora(Tehai tehai, InfoForResult ifr, Field field, int dapaiActor, int playerId, string pai)
        {
            if (dapaiActor == playerId)
            {
                return CanHoraIntsumo( tehai,  ifr,  field, dapaiActor, playerId, pai);
            }
            else
            {
                return CanHoraInRon(tehai, ifr, field, dapaiActor, playerId, pai);
            }
        }

        private bool CanHoraIntsumo(Tehai tehai, InfoForResult ifr, Field field, int dapaiActor, int playerId, string pai)
        {
            if( tehai.GetShanten() == -1 )
            {
                var horaResult = ResultCalclator.CalcHoraResult(tehai, ifr, field, pai);
                if (horaResult.yakuResult.HasYakuExcludeDora)
                {
                    return true;
                }
            }
            return false;
        }

        private bool CanHoraInRon(Tehai tehai, InfoForResult ifr, Field field, int dapaiActor, int playerId, string pai)
        {

            if (tehai.GetShanten(pai) == -1)
            {
                var horaResult = ResultCalclator.CalcHoraResult(tehai, ifr, field, pai);
                if (horaResult.yakuResult.HasYakuExcludeDora)
                {
                    return true;
                }
            }
            return false;
        }

    }
}


text:
package org.qfox.jestful.server.resolver;

import org.qfox.jestful.core.Action;
import org.qfox.jestful.core.Parameter;

/**
 * Created by yangchangpei on 17/5/2.
 */
public interface Resolver {

    boolean supports(Action action, Parameter parameter);

    void resolve(Action action, Parameter parameter) throws Exception;

}


text:
module JSON
  def self.is_json?(foo)
    begin
      return false unless foo.is_a?(String)
      JSON.parse(foo).all?
    rescue JSON::ParserError
      false
    end
  end
end


text:
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:exslt="http://exslt.org/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd" version="3.3" ID="P0b002ee180e5cf8d">
<name type="corporate">
 <namePart>United States Government Printing Office</namePart>
 <role>
  <roleTerm type="text" authority="marcrelator">printer</roleTerm>
  <roleTerm type="code" authority="marcrelator">prt</roleTerm>
</role>
 <role>
  <roleTerm type="text" authority="marcrelator">distributor</roleTerm>
  <roleTerm type="code" authority="marcrelator">dst</roleTerm>
</role>
</name>
<name type="corporate">
 <namePart>United States</namePart>
 <namePart>United States Court of Appeals for the Eighth Circuit</namePart>
 <role>
  <roleTerm type="text" authority="marcrelator">author</roleTerm>
  <roleTerm type="code" authority="marcrelator">aut</roleTerm>
</role>
 <description>Government Organization</description>
</name>
<typeOfResource>text</typeOfResource>
<genre authority="marcgt">government publication</genre>
<language>
 <languageTerm authority="iso639-2b" type="code">eng</languageTerm>
</language>
<extension>
 <collectionCode>USCOURTS</collectionCode>
 <category>Judicial Publications</category>
 <branch>judicial</branch>
 <dateIngested>2011-10-13</dateIngested>
</extension>
<originInfo>
 <publisher>Administrative Office of the United States Courts</publisher>
 <dateIssued encoding="w3cdtf">2010-01-25</dateIssued>
 <issuance>monographic</issuance>
</originInfo>
<physicalDescription>
 <note type="source content type">deposited</note>
 <digitalOrigin>born digital</digitalOrigin>
</physicalDescription>
<classification authority="sudocs">JU 2.11</classification>
<identifier type="uri">http://www.gpo.gov/fdsys/pkg/USCOURTS-ca8-09-01271</identifier>
<identifier type="local">P0b002ee180e5cf8d</identifier>
<recordInfo>
 <recordContentSource authority="marcorg">DGPO</recordContentSource>
 <recordCreationDate encoding="w3cdtf">2011-10-13</recordCreationDate>
 <recordChangeDate encoding="w3cdtf">2011-10-13</recordChangeDate>
 <recordIdentifier source="DGPO">USCOURTS-ca8-09-01271</recordIdentifier>
 <recordOrigin>machine generated</recordOrigin>
 <languageOfCataloging>
  <languageTerm authority="iso639-2b" type="code">eng</languageTerm>
</languageOfCataloging>
</recordInfo>
<accessCondition type="GPO scope determination">fdlp</accessCondition>
<extension>
 <docClass>USCOURTS</docClass>
 <accessId>USCOURTS-ca8-09-01271</accessId>
 <courtType>Appellate</courtType>
 <courtCode>ca8</courtCode>
 <courtCircuit>8th</courtCircuit>
 <courtSortOrder>1080</courtSortOrder>
 <caseNumber>09-01271</caseNumber>
 <natureSuitCode>440</natureSuitCode>
 <natureSuit>Other Civil Rights</natureSuit>
 <party firstName="Lynn" fullName="Lynn L. Blum" lastName="Blum" middleName="L." role="Not Party"></party>
 <party fullName="City of Schuyler" lastName="City of Schuyler" role="Not Party"></party>
 <party fullName="County of Colfax" lastName="County of Colfax" role="Not Party"></party>
 <party firstName="Greg" fullName="Greg E. Hajek" lastName="Hajek" middleName="E." role="Appellee"></party>
 <party firstName="Lennie" fullName="Lennie Hiltner" lastName="Hiltner" role="Not Party"></party>
 <party firstName="Jeff" fullName="Jeff Olson" lastName="Olson" role="Appellant"></party>
 <party firstName="Jerome" fullName="Jerome Stewart" lastName="Stewart" role="Not Party"></party>
 <party firstName="Susan" fullName="Susan L. Wallingford" lastName="Wallingford" middleName="L." role="Appellee"></party>
 <party firstName="Scott" fullName="Scott Weemer" lastName="Weemer" role="Not Party"></party>
</extension>
<titleInfo>
 <title>Susan Wallingford, et al v. Jeff Olson</title>
 <partNumber>09-01271</partNumber>
</titleInfo>
<location>
 <url access="object in context" displayLabel="Content Detail">http://www.gpo.gov/fdsys/pkg/USCOURTS-ca8-09-01271/content-detail.html</url>
</location>
<classification authority="sudocs">JU 2.11</classification>
<identifier type="preferred citation">09-01271;09-1271</identifier>
<name type="corporate">
 <namePart>United States Court of Appeals for the Eighth Circuit</namePart>
 <namePart>8th Circuit</namePart>
 <namePart></namePart>
 <affiliation>U.S. Courts</affiliation>
 <role>
  <roleTerm authority="marcrelator" type="text">author</roleTerm>
  <roleTerm authority="marcrelator" type="code">aut</roleTerm>
</role>
</name>
<name type="personal">
 <displayForm>Lynn L. Blum</displayForm>
 <namePart type="family">Blum</namePart>
 <namePart type="given">Lynn</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<name type="personal">
 <displayForm>City of Schuyler</displayForm>
 <namePart type="family">City of Schuyler</namePart>
 <namePart type="given"></namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<name type="personal">
 <displayForm>County of Colfax</displayForm>
 <namePart type="family">County of Colfax</namePart>
 <namePart type="given"></namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<name type="personal">
 <displayForm>Greg E. Hajek</displayForm>
 <namePart type="family">Hajek</namePart>
 <namePart type="given">Greg</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Appellee</description>
</name>
<name type="personal">
 <displayForm>Lennie Hiltner</displayForm>
 <namePart type="family">Hiltner</namePart>
 <namePart type="given">Lennie</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<name type="personal">
 <displayForm>Jeff Olson</displayForm>
 <namePart type="family">Olson</namePart>
 <namePart type="given">Jeff</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Appellant</description>
</name>
<name type="personal">
 <displayForm>Jerome Stewart</displayForm>
 <namePart type="family">Stewart</namePart>
 <namePart type="given">Jerome</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<name type="personal">
 <displayForm>Susan L. Wallingford</displayForm>
 <namePart type="family">Wallingford</namePart>
 <namePart type="given">Susan</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Appellee</description>
</name>
<name type="personal">
 <displayForm>Scott Weemer</displayForm>
 <namePart type="family">Weemer</namePart>
 <namePart type="given">Scott</namePart>
 <namePart type="termsOfAddress"></namePart>
 <description>Not Party</description>
</name>
<extension>
 <docClass>USCOURTS</docClass>
 <accessId>USCOURTS-ca8-09-01271</accessId>
 <courtType>Appellate</courtType>
 <courtCode>ca8</courtCode>
 <courtCircuit>8th</courtCircuit>
 <courtSortOrder>1080</courtSortOrder>
 <caseNumber>09-01271</caseNumber>
 <natureSuitCode>440</natureSuitCode>
 <natureSuit>Other Civil Rights</natureSuit>
 <party firstName="Lynn" fullName="Lynn L. Blum" lastName="Blum" middleName="L." role="Not Party"></party>
 <party fullName="City of Schuyler" lastName="City of Schuyler" role="Not Party"></party>
 <party fullName="County of Colfax" lastName="County of Colfax" role="Not Party"></party>
 <party firstName="Greg" fullName="Greg E. Hajek" lastName="Hajek" middleName="E." role="Appellee"></party>
 <party firstName="Lennie" fullName="Lennie Hiltner" lastName="Hiltner" role="Not Party"></party>
 <party firstName="Jeff" fullName="Jeff Olson" lastName="Olson" role="Appellant"></party>
 <party firstName="Jerome" fullName="Jerome Stewart" lastName="Stewart" role="Not Party"></party>
 <party firstName="Susan" fullName="Susan L. Wallingford" lastName="Wallingford" middleName="L." role="Appellee"></party>
 <party firstName="Scott" fullName="Scott Weemer" lastName="Weemer" role="Not Party"></party>
</extension>
<relatedItem type="constituent" ID="id-USCOURTS-ca8-09-01271-0" xlink:href="http://www.gpo.gov/fdsys/granule/USCOURTS-ca8-09-01271/USCOURTS-ca8-09-01271-0/mods.xml">
 <titleInfo>
  <title>Susan Wallingford, et al v. Jeff Olson</title>
  <subTitle>OPINION FILED - THE COURT: ROGER L. WOLLMAN, WILLIAM JAY RILEY and BOBBY E. SHEPHERD. William Jay Riley, Authoring Judge (PUBLISHED) [3627243] [09-1271]</subTitle>
  <partNumber>0</partNumber>
</titleInfo>
 <originInfo>
  <dateIssued>2010-01-25</dateIssued>
</originInfo>
 <relatedItem xlink:href="http://www.gpo.gov/fdsys/pkg/pdf/USCOURTS-ca8-09-01271-0.pdf" type="otherFormat">
  <identifier type="FDsys Unique ID">D09002ee180e646fd</identifier>
</relatedItem>
 <identifier type="uri">http://www.gpo.gov/fdsys/granule/USCOURTS-ca8-09-01271/USCOURTS-ca8-09-01271-0</identifier>
 <identifier type="former granule identifier">ca8-09-01271_0.pdf</identifier>
 <location>
  <url access="object in context" displayLabel="Content Detail">http://www.gpo.gov/fdsys/granule/USCOURTS-ca8-09-01271/USCOURTS-ca8-09-01271-0/content-detail.html</url>
  <url access="raw object" displayLabel="PDF rendition">http://www.gpo.gov/fdsys/pkg/USCOURTS-ca8-09-01271/pdf/USCOURTS-ca8-09-01271-0.pdf</url>
</location>
 <extension>
  <searchTitle>USCOURTS 09-01271; Susan Wallingford, et al v. Jeff Olson; </searchTitle>
  <courtName>United States Court of Appeals for the Eighth Circuit</courtName>
  <accessId>USCOURTS-ca8-09-01271-0</accessId>
  <sequenceNumber>0</sequenceNumber>
  <dateIssued>2010-01-25</dateIssued>
  <docketText>OPINION FILED - THE COURT: ROGER L. WOLLMAN, WILLIAM JAY RILEY and BOBBY E. SHEPHERD. William Jay Riley, Authoring Judge (PUBLISHED) [3627243] [09-1271]</docketText>
</extension>
</relatedItem>
</mods>


text:
id: 587d7daf367417b2b2512b80
title: Remember to Set the Constructor Property when Changing the Prototype
challengeType: 1
videoUrl: ''
localeTitle: 请记住在更改原型时设置构造函数属性
---

## Description

将prototype手动设置为新对象有一个至关重要的副作用。它擦除了constructor属性!上一个挑战中的代码将为duck打印以下内容:
执行console.log(duck.constructor)
 //打印'undefined' - 哎呀!
要解决此问题,每当手动将原型设置为新对象时,请记住定义constructor属性:
 Bird.prototype = {
构造函数:Bird,//定义构造函数属性
 numLegs:2,
吃:function(){
 console.log("nom nom nom");
 },
 describe:function(){
 console.log("我的名字是"+ this.name);
 }
 };

## Instructions

在Dog prototype上定义constructor属性。

## Tests

```yml
tests:
  - text: <code>Dog.prototype</code>应该设置<code>constructor</code>属性。
    testString: assert(Dog.prototype.constructor === Dog);

```

## Challenge Seed

```js
function Dog(name) {
  this.name = name;
}

// Modify the code below this line
Dog.prototype = {

  numLegs: 2,
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};

```

## Solution

```js
// solution required
```


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>
org.apache.solr.handler.dataimport.config (Solr 4.3.1 API)
</TITLE>

<META NAME="date" CONTENT="2013-06-09">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

      Overview
    Package
      Class
      Use
      Tree
      Deprecated
      Help

 PREV PACKAGE
 NEXT PACKAGE

  FRAMES
 NO FRAMES

  All Classes

Package org.apache.solr.handler.dataimport.config

Utility classes for parsing & modeling DIH configuration.

See:

          Description

Class Summary

ConfigNameConstants

ConfigParseUtil

DIHConfiguration

 Mapping for data-config.xml

Document

 Mapping for data-config.xml

Entity

EntityField

EntityField.Builder

Field

Field.Builder

PropertyWriter

Script

Package org.apache.solr.handler.dataimport.config Description

Utility classes for parsing & modeling DIH configuration.

      Overview
    Package
      Class
      Use
      Tree
      Deprecated
      Help

 PREV PACKAGE
 NEXT PACKAGE

  FRAMES
 NO FRAMES

  All Classes

          Copyright © 2000-2013 Apache Software Foundation.  All Rights Reserved.