<s> from functools import partial <EOL> from django . utils . safestring import mark_safe <EOL> def int_or_default ( value , default = None ) : <EOL> try : <EOL> return abs ( int ( value ) ) <EOL> except ( ValueError , TypeError ) : <EOL> return default <EOL> int_or_zero = partial ( int_or_default , default = <NUM_LIT:0> ) <EOL> def wrap_tag ( text , link , tag = '<STR_LIT:a>' ) : <EOL> template = '<STR_LIT>' <EOL> attr = '<STR_LIT>' if tag == '<STR_LIT:a>' else '<STR_LIT>' <EOL> return mark_safe ( template % { <EOL> '<STR_LIT>' : tag , <EOL> '<STR_LIT:text>' : text , <EOL> '<STR_LIT>' : link , <EOL> '<STR_LIT>' : attr <EOL> } ) <EOL> def replace_with_link ( source , text , url , tag ) : <EOL> lower_source = source . lower ( ) <EOL> text = text . lower ( ) <EOL> if text not in lower_source : <EOL> return <EOL> position = lower_source . index ( text ) <EOL> left = source [ : position ] <EOL> middle = source [ position : position + len ( text ) ] <EOL> right = source [ position + len ( text ) : ] <EOL> token_start , token_end = '<STR_LIT:<>' + tag , '<STR_LIT>' + tag <EOL> if ( left . count ( token_start ) != <EOL> left . count ( token_end ) ) : <EOL> return <EOL> link = ( left + <EOL> wrap_tag ( middle , url , tag ) + <EOL> right ) <EOL> return link </s>
