Class: Tidy

Inherits:
Object
  • Object
show all
Defined in:
ext/tidy/ruby-tidy.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



198
199
200
201
# File 'ext/tidy/ruby-tidy.c', line 198

static VALUE rb_tidy_init(VALUE self)
{
  return self;
}

Instance Attribute Details

#accessObject

#errorsObject (readonly)

Class Method Details

.new(*args) ⇒ Object

create a new tidy doc



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/tidy/ruby-tidy.c', line 92

static VALUE rb_tidy_new(int argc, VALUE *argv, VALUE class)
{
  TidyDoc tdoc = tidyCreate();
  VALUE options;
  VALUE access = INT2NUM(4);
  VALUE errors = rb_ary_new();

  VALUE self = Data_Wrap_Struct(class, 0, rb_tidy_free, (struct _TidyDoc *)tdoc);

  rb_scan_args(argc, argv, "01", &options);
  options = NIL_P(options) ? rb_hash_new() : options;

  rb_iv_set(self, "@options", options);
  rb_iv_set(self, "@access", access);
  rb_iv_set(self, "@errors", errors);

  rb_iterate(rb_each, options, rb_tidy_set_option, self);

  rb_obj_call_init(self, 0, NULL);

  return self;
}

.open(options) ⇒ Object

Create a tidy object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/tidy/ruby-tidy.c', line 204

static VALUE rb_tidy_open(VALUE class, VALUE options)
{
  VALUE args[1];
  VALUE tidy;

  args[0] = options;
  tidy  = rb_tidy_new(1, args, class);

  if (rb_block_given_p()) {
    rb_yield(tidy);
  }

  return tidy;
}

.pathObject

For sideways compatibility



230
231
232
233
234
235
# File 'ext/tidy/ruby-tidy.c', line 230

static VALUE rb_tidy_path_get(VALUE self)
{
  VALUE path;
  path = rb_cv_get(self, "@@path");
  return path;
}

.path=(path) ⇒ Object

For sideways compatibility



238
239
240
241
242
# File 'ext/tidy/ruby-tidy.c', line 238

static VALUE rb_tidy_path_set(VALUE self, VALUE path)
{
  rb_cv_set(self, "@@path", path);
  return Qnil;
}

Instance Method Details

#clean(input) ⇒ Object

Given a string, returns the string after tidying



220
221
222
223
224
225
226
227
# File 'ext/tidy/ruby-tidy.c', line 220

static VALUE rb_tidy_clean(VALUE self, VALUE input)
{
  VALUE array;

  array = rb_tidy_parse(self, input);

  return rb_ary_entry(array, 1);
}

#parse(input) ⇒ Object

parse the given input and return the tidy errors and output



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/tidy/ruby-tidy.c', line 116

static VALUE rb_tidy_parse(VALUE self, VALUE input)
{
  VALUE array;
  VALUE access;
  VALUE errors;
  VALUE options;

  TidyDoc tdoc;
  TidyBuffer output;
  TidyBuffer errbuf;
  int status          = 0;

  int contentErrors   = 0;
  int contentWarnings = 0;
  int accessWarnings  = 0;

  /* See platform.h, opaque_type for typedef convention */
  Data_Get_Struct(self, struct _TidyDoc, tdoc);

  tidyBufInit( &output );
  tidyBufInit( &errbuf );

  array = rb_ary_new();

  status = tidySetErrorBuffer( tdoc, &errbuf );

  if (status >= 0) {

    int is_input_source = 0;

    is_input_source =
      rb_respond_to(input, rb_intern("eof")) == Qtrue &&
      rb_respond_to(input, rb_intern("getc")) == Qtrue &&
      rb_respond_to(input, rb_intern("ungetc")) == Qtrue;

    if (is_input_source != 0) {
      TidyInputSource source;

      tidyInitSource(&source, (void *)&input,
        (TidyGetByteFunc)rb_tidyGetByte,
        (TidyUngetByteFunc)rb_tidyUngetByte,
        (TidyEOFFunc)rb_tidyIsEOF);

      status = tidyParseSource(tdoc, &source);
    } else {
      status = tidyParseString(tdoc, StringValuePtr(input));
    }
  }

  if (status >= 0)
    status = tidyCleanAndRepair( tdoc );
  if (status >= 0)
    status = tidyRunDiagnostics( tdoc );
  if (status >= 0)
    tidyErrorSummary( tdoc );
  if (status >= 0)
    tidyGeneralInfo( tdoc );

  if (status >= 0)
    status = tidySaveBuffer( tdoc, &output );

  contentErrors   = tidyErrorCount( tdoc );
  contentWarnings = tidyWarningCount( tdoc );
  accessWarnings  = tidyAccessWarningCount( tdoc );

  if (contentErrors > 0 || contentWarnings > 0) {
    errors = rb_str_new2(errbuf.bp);
  } else {
    errors = rb_str_new2("");
  }

  rb_iv_set(self, "@errors", errors);

  rb_ary_store(array, 0, errors);
  rb_ary_store(array, 1, rb_str_new2(output.bp));

  tidyBufFree( &output );
  tidyBufFree( &errbuf );

  return array;
}