~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to phabricator/src/infrastructure/markup/rule/PhabricatorObjectRemarkupRule.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-11-01 23:20:06 UTC
  • mfrom: (0.6.1) (0.5.1) (1.2.1) (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20141101232006-9rsc6uy3t32oty8r
Tags: 0~git20141101-1
* New upstream version
* renamed translation po files so that they are used (closes: #767636)

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
  }
96
96
 
97
97
  public function apply($text) {
98
 
    $prefix = $this->getObjectNamePrefix();
99
 
    $prefix = preg_quote($prefix, '@');
100
 
    $id = $this->getObjectIDPattern();
101
 
 
102
98
    $text = preg_replace_callback(
103
 
      '@\B{'.$prefix.'('.$id.')((?:[^}\\\\]|\\\\.)*)}\B@u',
 
99
      $this->getObjectEmbedPattern(),
104
100
      array($this, 'markupObjectEmbed'),
105
101
      $text);
106
102
 
 
103
    $text = preg_replace_callback(
 
104
      $this->getObjectReferencePattern(),
 
105
      array($this, 'markupObjectReference'),
 
106
      $text);
 
107
 
 
108
    return $text;
 
109
  }
 
110
 
 
111
  private function getObjectEmbedPattern() {
 
112
    $prefix = $this->getObjectNamePrefix();
 
113
    $prefix = preg_quote($prefix);
 
114
    $id = $this->getObjectIDPattern();
 
115
 
 
116
    return '(\B{'.$prefix.'('.$id.')((?:[^}\\\\]|\\\\.)*)}\B)u';
 
117
  }
 
118
 
 
119
  private function getObjectReferencePattern() {
 
120
    $prefix = $this->getObjectNamePrefix();
 
121
    $prefix = preg_quote($prefix);
 
122
 
 
123
    $id = $this->getObjectIDPattern();
 
124
 
107
125
    // If the prefix starts with a word character (like "D"), we want to
108
126
    // require a word boundary so that we don't match "XD1" as "D1". If the
109
127
    // prefix does not start with a word character, we want to require no word
121
139
    // The "\b" allows us to link "(abcdef)" or similar without linking things
122
140
    // in the middle of words.
123
141
 
124
 
    $text = preg_replace_callback(
125
 
      '((?<![#-])'.$boundary.$prefix.'('.$id.')(?:#([-\w\d]+))?(?!\w))u',
126
 
      array($this, 'markupObjectReference'),
127
 
      $text);
128
 
 
129
 
    return $text;
 
142
    return '((?<![#-])'.$boundary.$prefix.'('.$id.')(?:#([-\w\d]+))?(?!\w))u';
 
143
  }
 
144
 
 
145
 
 
146
  /**
 
147
   * Extract matched object references from a block of text.
 
148
   *
 
149
   * This is intended to make it easy to write unit tests for object remarkup
 
150
   * rules. Production code is not normally expected to call this method.
 
151
   *
 
152
   * @param   string  Text to match rules against.
 
153
   * @return  wild    Matches, suitable for writing unit tests against.
 
154
   */
 
155
  public function extractReferences($text) {
 
156
    $embed_matches = null;
 
157
    preg_match_all(
 
158
      $this->getObjectEmbedPattern(),
 
159
      $text,
 
160
      $embed_matches,
 
161
      PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
 
162
 
 
163
    $ref_matches = null;
 
164
    preg_match_all(
 
165
      $this->getObjectReferencePattern(),
 
166
      $text,
 
167
      $ref_matches,
 
168
      PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
 
169
 
 
170
    $results = array();
 
171
    $sets = array(
 
172
      'embed' => $embed_matches,
 
173
      'ref' => $ref_matches,
 
174
    );
 
175
    foreach ($sets as $type => $matches) {
 
176
      $formatted = array();
 
177
      foreach ($matches as $match) {
 
178
        $format = array(
 
179
          'offset' => $match[1][1],
 
180
          'id' => $match[1][0],
 
181
        );
 
182
        if (isset($match[2][0])) {
 
183
          $format['tail'] = $match[2][0];
 
184
        }
 
185
        $formatted[] = $format;
 
186
      }
 
187
      $results[$type] = $formatted;
 
188
    }
 
189
 
 
190
    return $results;
130
191
  }
131
192
 
132
193
  public function markupObjectEmbed($matches) {