o xgL>@sddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZmZmZddlmZddlmZmZmZed d Zed ejZed ejZGd ddZGdddZeZedejZ edejZ!edZ"edZ#eddZ$GdddeZ%eddZ&eedfddZ'eddZ(ed d!Z)d"d#Z*Gd$d%d%e Z+d&d'Z,ed(ej-Z.d)d*Z/d+d,Z0ed-Z1ed.d/Z2ed0d1Z3ed9d3d4Z4d5d6Z5d7d8Z6ee6e7Z8dS):N)GzipFile)BytesIO)SuspiciousFileOperation)RemovedInDjango40Warning)SimpleLazyObjectkeep_lazy_textlazy)_lazy_re_compile)gettext gettext_lazypgettextcCs$|ot|dt|ddS)z(Capitalize the first letter of a string.rN)strupper)xr3/usr/lib/python3/dist-packages/django/utils/text.pycapfirsts$rz ([^<>\s]+)z <|([^<>\s]+)c@seZdZeddZdS) WordsRegexcCsnt||}|dus|ddur|S|d|d}|dkr't||dS|d7}t||d||S)Nr >r)re_prtsearchfindendre_notag FakeMatchstart)textpospartialrrrrr&s zWordsRegex.searchN)__name__ __module__ __qualname__ staticmethodrrrrrr%src@s.eZdZddgZd ddZddZdd Zd S) r_text_endrcCs|dksJd|jS)Nrz'This specific object takes only group=0)r%selfgrouprrrr;sz FakeMatch.endcCs"|dkrdS|dksJd|jS)Nr rz.This specific object takes only group in {0,1})r$r&rrr __getitem__?szFakeMatch.__getitem__cCs|||_|_dSN)r$r%)r'rrrrr__init__EszFakeMatch.__init__N)r)r r!r" __slots__rr)r+rrrrr8s   rz <[^>]+?>|(.)z<(/)?(\S+?)(?:(\s*/)|\s.*?)?>z\r\n|\rz&(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))cCs>tj|ddd}g}|dD] }|||qd|S)aC A word-wrap function that preserves existing line breaks. Expects that existing line breaks are posix newlines. Preserve all white space except added line breaks consume the space on which they break the line. Don't wrap long words, thus the output text may have lines longer than ``width``. F)widthbreak_long_wordsbreak_on_hyphensT )textwrap TextWrapper splitlinesextendwrapjoin)rr-wrapperresultlinerrrr5Ss  r5csZeZdZdZdZfddZdddZdd d Zd d Zdd dZ ddZ ddZ Z S) Truncatorz An object used to truncate text, either by characters or words. When truncating HTML text (either chars or words), input will be limited to at most `MAX_LENGTH_HTML` characters. i@KLcstfdddS)NcstSr*rrrrrvsz$Truncator.__init__..)superr+)r'r __class__r<rr+uszTruncator.__init__NcCs@|dur tdd}d|vr|d|iS||r|Sd||fS)Nz%String to return when truncating textu%(truncated_text)s…z%(truncated_text)struncated_textz%s%s)r endswith)r'rtruncaterrradd_truncation_textxs   zTruncator.add_truncation_textFcCsx|t|}td|j}|}|d|D]}t|s(|d8}|dkr(nq|r4|||||dS|||||S)a Return the text truncated to be no longer than the specified number of characters. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to a translatable string of an ellipsis. NFCr rF) _setupint unicodedata normalize_wrappedrD combining_truncate_html _text_chars)r'numrChtmllengthr truncate_lencharrrrcharss zTruncator.charsc Csjd}d}t|D]*\}}t|rq|d7}|dur ||kr |}||kr2||d|p+d|Sq|S)z2Truncate a string after a certain number of chars.rNr ) enumeraterIrLrD) r'rQrCrrRs_len end_indexirSrrrrNs zTruncator._text_charscCs4|t|}|r||||j|dS|||S)z Truncate a string after a certain number of words. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to ellipsis. T)rGrHrMrK _text_words)r'rOrCrPrQrrrwordss  zTruncator.wordscCs>|j}t||kr|d|}|d||Sd|S)zk Truncate a string after a certain number of words. Strip newlines in the string. N )rKsplitlenrDr6)r'rQrCrZrrrrYs    zTruncator._text_wordscCs||r|dkrdSd}t||jkr|d|j}d}d}d}d} d} g} |r(tnt} | |kr| ||} | s7nZ| d}| drK| d7} | |krJ|} q*t| d}|rX| |krYq*|\}}}| }|sj||vrkn"|rz| |}Wn t y}Ynw| |dd} n| d|| |ks.| d|}| |kr|r|r||7}|S|d| }|r||7}| D]}|d|7}q|S) a Truncate HTML to a certain number of chars (not counting tags and comments), or, if words is True, then to a certain number of words. Close opened tags if they were correctly closed in the given HTML. Preserve newlines in the HTML. rrFFNT) brcollinkbaseimgparamareahrinputr z)r]MAX_LENGTH_HTMLre_wordsre_charsrrre_tagmatchgroupslowerindex ValueErrorinsertrD)r'rQrCrrRrZ size_limitedhtml4_singletsr end_text_pos current_len open_tagsregexmtag closing_tagtagname self_closingrX truncate_textoutrrrrMsb         $ zTruncator._truncate_htmlr*)NF) r r!r"__doc__rgr+rDrTrNrZrYrM __classcell__rrr?rr:js      r:cCs:t|dd}tdd|}|dvrtd||S)aY Return the given string converted to a string that can be used for a clean filename. Remove leading and trailing spaces; convert other spaces to underscores; and remove anything that is not an alphanumeric, dash, underscore, or dot. >>> get_valid_filename("john's portrait in 2004.jpg") 'johns_portrait_in_2004.jpg' r[_z (?u)[^-\w.]rF>..rF.z$Could not derive file name from '%s')rstripreplaceresubr)namesrrrget_valid_filenames   rorcCsV|sdSt|dkrt|dSdtddd|dd Dt|t|d fS) a >>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c or d' >>> get_text_list(['a', 'b', 'c'], 'and') 'a, b and c' >>> get_text_list(['a', 'b'], 'and') 'a and b' >>> get_text_list(['a']) 'a' >>> get_text_list([]) '' rFr rz%s %s %sz, css|]}t|VqdSr*r;).0rXrrr Asz get_text_list..N)r]rrr6)list_ last_wordrrr get_text_list-s  .rcCstdt|S)z*Normalize CRLF and CR newlines to just LF.r0) re_newlinesrrr<rrrnormalize_newlinesEsrc siddddddddddddd d d d d d d ddddddddddddddddddddddddd dfdd|DS)z@Convert a phone number with letters into its numeric equivalent.a2bcd3efg4hrXj5klrw6nop7q89) rrtuvwryzrFc3s|] }||VqdSr*)get)rr char2numberrrrTsz phone2numeric..)r6rm)phonerrr phone2numericKsN rcCsNt}tdd|dd}||Wd|S1swY|SNwbr)mode compresslevelfileobjmtime)rrwritegetvalue)rzbufzfilerrrcompress_stringYs  rc@seZdZddZdS)StreamingBuffercCs|}|d||S)Nr)rseekrC)r'retrrrreadas zStreamingBuffer.readN)r r!r"rrrrrr`s rccsvt}tdd|dd }|V|D]}|||}|r$|VqWdn1s/wY|VdSr)rrrr)sequencebufritemdatarrrcompress_sequenceis  rz ((?: [^\s'"]* (?: (?:"(?:[^"\\]|\\.)*" | '(?:[^'\\]|\\.)*') [^\s'"]* )+ ) | \S+) ccs$tt|D]}|dVqdS)a Generator that splits a string by spaces, leaving quoted phrases together. Supports both single and double quotes, and supports escaping quotes with backslashes. In the output, strings will keep their initial and trailing quote marks and escaped quotes will remain escaped (the results can then be further processed with unescape_string_literal()). >>> list(smart_split(r'This is "a person\'s" test.')) ['This', 'is', '"a person\\\'s"', 'test.'] >>> list(smart_split(r"Another 'person\'s' test.")) ['Another', "'person\\'s'", 'test.'] >>> list(smart_split(r'A "\"funky\" style" test.')) ['A', '"\\"funky\\" style"', 'test.'] rN)smart_split_refinditerr)rbitrrr smart_splits rcCs|d}|ddkr7|dd}z|ddvr!t|ddd}nt|}t|WSty6|dYSwz ttjj|WStyM|dYSw)Nr r#xX)rHchrrorPentitiesname2codepointKeyError)rkrrrrr_replace_entitys         rz#&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));cCs tjdtddttt|S)NzPdjango.utils.text.unescape_entities() is deprecated in favor of html.unescape().) stacklevel)warningswarnr _entity_rerrrr<rrrunescape_entitiess rcCsP|ddvs|d|dkrtd||d}|ddd||ddS) al Convert quoted string literals to unquoted strings with escaped quotes and backslashes unquoted:: >>> unescape_string_literal('"abc"') 'abc' >>> unescape_string_literal("'abc'") 'abc' >>> unescape_string_literal('"a \"bc\""') 'a "bc"' >>> unescape_string_literal("'\'ab\' c'") "'ab' c" rz"'rzNot a string literal: %rr z\%sz\\\)ror)rquoterrrunescape_string_literals  rFcCsZt|}|r td|}n td|ddd}tdd|}tdd|d S) a Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace, dashes, and underscores. NFKCNFKDasciiignorez[^\w\s-]rFz[-\s]+-z-_) rrIrJencodedecoderrrmr)value allow_unicoderrrslugifys rcCstd|S)zQ Split CamelCase and convert to lowercase. Strip surrounding whitespace. z \1) re_camel_caserrrm)rrrrcamel_case_to_spacessrcOs|j|i|S)zk Apply str.format() on 'format_string' where format_string, args, and/or kwargs might be lazy. )format) format_stringargskwargsrrr _format_lazysr)F)9 html.entitiesrPrr1rIrgzipriordjango.core.exceptionsrdjango.utils.deprecationrdjango.utils.functionalrrrdjango.utils.regex_helperr django.utils.translationr rr r rSrrrrrhrirjrrr5r:rrrrrrrVERBOSErrrrrrrrrr format_lazyrrrrsj           3